fork download
  1. def main():
  2. #fin = open("flip.in","r")
  3. #nm = [int(i) for i in fin.readline().split()]
  4. nm = [int(i) for i in input().split()]
  5. n = nm[0]
  6. m = nm[1]
  7. matrix = [list(map(int, input().split())) for _ in range(n)]
  8. #print(matrix)
  9. sum_max = 0
  10.  
  11. def solve_flip(stack):
  12.  
  13. #declare the variable as nonlocal to modify the outer variable
  14. nonlocal sum_max
  15. #declare a variable that holds local stotal
  16. stotal = 0
  17. #make a copy of the matrix
  18. matrix2 = [row[:] for row in matrix]
  19.  
  20. for line in stack:
  21. for j in range(m):
  22. matrix2[line][j] *= -1
  23.  
  24. for j in range(m):
  25. s = 0
  26. for i in range(n):
  27. s += matrix2[i][j]
  28. if s<0:
  29. s *= -1
  30. stotal += s
  31.  
  32. if sum_max < stotal:
  33. sum_max = stotal
  34.  
  35. size = 2**n
  36. mask = 1
  37. for i in range(1,size):
  38. #empty stack
  39. stack = []
  40. for j in range(n):
  41. if (mask<<j)&i:
  42. #print(j, end = " ")
  43. stack.append(j)
  44. solve_flip(stack)
  45. #fout = open("flip.out","w")
  46. print(sum_max)
  47. #fout.write(str(sum_max))
  48. main()
Success #stdin #stdout 0.04s 9896KB
stdin
5 3
4 -2 2
3 -1 5
2 0 -3
4 1 -3
5 -3 2
stdout
28