#C1099. Matrix Inversion for Maximum Sum
Matrix Inversion for Maximum Sum
Matrix Inversion for Maximum Sum
Given an (n \times m) matrix, you are allowed to choose exactly one contiguous submatrix and invert the sign of every element in that submatrix. Your task is to compute the maximum possible sum of all the elements in the matrix after performing this inversion operation exactly once.
More formally, let (S) be the sum of all elements in the matrix and let (S_{sub}) be the sum of the elements in the chosen submatrix. After inversion, the new total sum becomes (S - 2 \times S_{sub}). You need to choose a submatrix such that this value is maximized.
Note that the submatrix you choose must be contiguous and can range from a single element to the entire matrix.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains two integers (n) and (m) representing the number of rows and columns of the matrix.
- The next (n) lines each contain (m) space-separated integers, representing the elements of the matrix.
outputFormat
Output a single integer which is the maximum sum obtainable by inverting exactly one contiguous submatrix.## sample
3 3
1 2 3
4 -5 -6
7 8 9
45