We have an n x n matrix and can pick any two adjacent cells (sharing a border), then flip both their signs. We can repeat this as many times as we want. The goal is to maximize the total sum of all elements.
Consider what one operation does to the sign of a negative number. If cell (i, j) is negative, flipping it with a neighbor makes (i, j) positive and turns the neighbor negative. Flip that neighbor with its own neighbor and the negative sign moves again. A chain of these flips slides a single negative sign from any cell to any other cell in the matrix, since the grid is connected.
When two negatives meet at adjacent cells, flipping that pair makes both positive. Negatives therefore cancel two at a time. With an even number of negatives, every one can be paired off and cancelled, leaving all elements positive. With an odd number, one negative always remains, and the best choice is to leave it on the element with the smallest absolute value.
-10^5 <= matrix[i][j] <= 10^5 and up to 62,500 elements. The maximum possible sum is 62,500 x 100,000 = 6.25 x 10^9, which exceeds the range of a 32-bit integer. The accumulator must be a 64-bit type.The operation never changes the absolute value of any element, only its sign. So the largest sum we could ever reach is the sum of all absolute values, which happens when every element is positive. The question becomes whether the operation can drive every element positive, and if not, what the smallest unavoidable penalty is.
Two facts settle it. The first is that a single flip moves a negative sign rather than removing it: flipping a negative cell with a positive neighbor leaves one negative cell, shifted by one position. Chaining these flips slides a negative sign along any path through the connected grid, so a negative can be relocated to any cell. The second is that flipping two adjacent negatives turns both positive, removing two negatives at once.
Together these mean negatives are removed in pairs. Slide any two negatives until they are adjacent, then cancel them. With an even number of negatives, every one is paired off and the whole matrix becomes positive, giving a sum equal to the total of absolute values. With an odd number, one negative cannot be paired and must remain. To lose as little as possible, that negative belongs on the element with the smallest absolute value: keeping a value v negative instead of positive costs 2 * |v|, so the penalty is minimized by choosing the smallest |v|.
A zero is the smallest possible absolute value, so when one is present the odd-count penalty is 2 * 0 = 0. The formula below handles that case without any special branch.
The full procedure:
Every flip preserves the parity of the negative count. Flipping two cells either turns two negatives positive (count drops by 2), two positives negative (count rises by 2), or one of each (count unchanged). All three keep the count's parity fixed, so a configuration with all elements positive is reachable only when the starting count of negatives is even.
When the count is odd, at least one negative must remain, and the configuration with exactly one negative on the smallest-absolute-value element is reachable (slide the surviving negative there). No configuration loses less, so this is optimal.
totalAbsSum = 0, minAbsValue = infinity, and negativeCount = 0.totalAbsSum.minAbsValue if this element's absolute value is smaller.negativeCount.negativeCount is even, return totalAbsSum.negativeCount is odd, return totalAbsSum - 2 * minAbsValue.Loading animation...