Question from semifinals of techgig code gladiators

There are D doctors and P patients. The cost taken by each doctors for each patients is given in a 2D List. I have to assign the patients to the doctors in a consecutive manner. For example, if there are 4 patients and 2 doctors then P1 and P2 to doctor 1 and P3 and P4 to doctor 2 is valid. P1 and P3 to doc 1 and P2 and P4 to doc 2 is invalid.

input:
4 2
2 2 2 2
3 1 2 3

output:
8

Explanation:
P1,P2 to doctor 2 and P3,P4 to doctor 1 (or)
P1,P2,P3 to doctor 2 and P4 to doctor 1 (or)
p1 to doctor 1 and and P2,P3,P4 to doctor 2 (or)
All Patients to doctor 1
All cases produces the same cost of 8

please help me in this question that how to solve this.