Pagini recente » Cod sursa (job #1985510) | Cod sursa (job #496596) | Cod sursa (job #3168678) | Cod sursa (job #1588453) | Cod sursa (job #3158525)
def maximize_sum(N, M, table):
max_sum = float('-inf')
for row_mask in range(1 << N):
for col_mask in range(1 << M):
flipped_table = [list(row) for row in table]
# Flip rows based on row_mask
for i in range(N):
if (row_mask & (1 << i)) != 0:
for j in range(M):
flipped_table[i][j] *= -1
# Flip columns based on col_mask
for j in range(M):
if (col_mask & (1 << j)) != 0:
for i in range(N):
flipped_table[i][j] *= -1
# Calculate the sum of elements in the modified table
current_sum = sum(sum(row) for row in flipped_table)
max_sum = max(max_sum, current_sum)
return max_sum
# Read input from the file
with open("flip.in", "r") as file:
N, M = map(int, file.readline().split())
table = [list(map(int, file.readline().split())) for _ in range(N)]
# Calculate the maximum sum
result = maximize_sum(N, M, table)
# Write the result to the output file
with open("flip.out", "w") as file:
file.write(str(result) + "\n")