from numpy import matrix
import numpy as np
n = 2
k = 4
A = matrix([[1,3],
[2,4]
])
sc = [0]*n
sr = [0]*n
for i in range(0,n):
sc[i] = sum( A.A[:,i] )
sr[i] = sum( A.A[i,:] )
output=0
def whichmin(c,r, iter):
if(iter < 0):
return 1
r[np.argmin(r)] += n
c[np.argmin(c)] += n
if ( np.amin(c) < np.amin(r) ):
return 1
elif ( np.amin(r) < np.amin(c) ):
return 0
else :
return whichmin(c,r, (iter -1) )
for j in range(0,k):
minc = np.amin(sc)
minr = np.amin(sr)
if (minc < minr):
incrindex = np.argmin(sc)
sc[incrindex]+=n
sr = [x+1 for x in sr]
output += minc
elif (minr < minc):
incrindex = np.argmin(sr)
sr[incrindex]+=n
sc = [x+1 for x in sc]
output += minr
else :
min = whichmin(sc.copy(),sr.copy(), (n - i) )
if (min==1):
incrindex = np.argmin(sc)
sc[incrindex]+=n
sr = [x+1 for x in sr]
output += minc
else:
incrindex = np.argmin(sr)
sr[incrindex]+=n
sc = [x+1 for x in sc]
output += minr
print(output)
print(sc)
print(sr)
ZnJvbSBudW1weSBpbXBvcnQgbWF0cml4CmltcG9ydCBudW1weSBhcyBucApuID0gMgprID0gNApBID0gbWF0cml4KFtbMSwzXSwKCQkJWzIsNF0KCQkJXSkKCQkJCnNjID0gWzBdKm4Kc3IgPSBbMF0qbgpmb3IgaSBpbiByYW5nZSgwLG4pOgoJc2NbaV0gPSBzdW0oIEEuQVs6LGldICkKCXNyW2ldID0gc3VtKCBBLkFbaSw6XSApCm91dHB1dD0wCgpkZWYgd2hpY2htaW4oYyxyLCBpdGVyKToKCWlmKGl0ZXIgPCAwKToKCQlyZXR1cm4gMQoJcltucC5hcmdtaW4ocildICs9IG4KCWNbbnAuYXJnbWluKGMpXSArPSBuCglpZiAoIG5wLmFtaW4oYykgPCBucC5hbWluKHIpICk6CgkJcmV0dXJuIDEKCWVsaWYgKCBucC5hbWluKHIpIDwgbnAuYW1pbihjKSApOgoJCXJldHVybiAwCgllbHNlIDoKCQlyZXR1cm4gd2hpY2htaW4oYyxyLCAoaXRlciAtMSkgKQoKZm9yIGogaW4gcmFuZ2UoMCxrKToKCW1pbmMgPSBucC5hbWluKHNjKQoJbWluciA9IG5wLmFtaW4oc3IpCglpZiAobWluYyA8IG1pbnIpOgoJCWluY3JpbmRleCA9IG5wLmFyZ21pbihzYykKCQlzY1tpbmNyaW5kZXhdKz1uCgkJc3IgPSBbeCsxIGZvciB4IGluIHNyXQoJCW91dHB1dCArPSBtaW5jCgllbGlmIChtaW5yIDwgbWluYyk6CgkJaW5jcmluZGV4ID0gbnAuYXJnbWluKHNyKQoJCXNyW2luY3JpbmRleF0rPW4KCQlzYyA9IFt4KzEgZm9yIHggaW4gc2NdCgkJb3V0cHV0ICs9IG1pbnIKCWVsc2UgOgoJCW1pbiA9IHdoaWNobWluKHNjLmNvcHkoKSxzci5jb3B5KCksIChuIC0gaSkgKQoJCWlmIChtaW49PTEpOgoJCQlpbmNyaW5kZXggPSBucC5hcmdtaW4oc2MpCgkJCXNjW2luY3JpbmRleF0rPW4KCQkJc3IgPSBbeCsxIGZvciB4IGluIHNyXQoJCQlvdXRwdXQgKz0gbWluYwoJCWVsc2U6CgkJCWluY3JpbmRleCA9IG5wLmFyZ21pbihzcikKCQkJc3JbaW5jcmluZGV4XSs9bgoJCQlzYyA9IFt4KzEgZm9yIHggaW4gc2NdCgkJCW91dHB1dCArPSBtaW5yCgpwcmludChvdXRwdXQpCnByaW50KHNjKQpwcmludChzcik=
[3, 7]
[4, 6]
----------
[5, 7]
[5, 7]
----------
[7, 7]
[6, 8]
----------
[8, 8]
[8, 8]
----------
[10, 8]
22
[9, 9]