<p>You are given an <code>n x n</code> integer <code>matrix</code>. You can do the following operation <strong>any</strong> number of times:</p>

<ul>
	<li>Choose any two <strong>adjacent</strong> elements of <code>matrix</code> and <strong>multiply</strong> each of them by <code>-1</code>.</li>
</ul>

<p>Two elements are considered <strong>adjacent</strong> if and only if they share a <strong>border</strong>.</p>

<p>Your goal is to <strong>maximize</strong> the summation of the matrix&#39;s elements. Return <em>the <strong>maximum</strong> sum of the matrix&#39;s elements using the operation mentioned above.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex1.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> matrix = [[1,-1],[-1,1]]
<strong>Output:</strong> 4
<b>Explanation:</b> We can follow the following steps to reach sum equals 4:
- Multiply the 2 elements in the first row by -1.
- Multiply the 2 elements in the first column by -1.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/16/pc79-q2ex2.png" style="width: 321px; height: 121px;" />
<pre>
<strong>Input:</strong> matrix = [[1,2,3],[-1,-2,-3],[1,2,3]]
<strong>Output:</strong> 16
<b>Explanation:</b> We can follow the following step to reach sum equals 16:
- Multiply the 2 last elements in the second row by -1.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == matrix.length == matrix[i].length</code></li>
	<li><code>2 &lt;= n &lt;= 250</code></li>
	<li><code>-10<sup>5</sup> &lt;= matrix[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def maxMatrixSum(self, matrix: List[List[int]]) -> int:
        # Observations:
        #   Can 'move' a negative sign to any positive number as long as there is a path of positive numbers to it
        #   If there are two adjacent negative numbers, we can eliminate them

        # Strategy:
        #   1. Eliminate all adjacent negative numbers
        #   2. Pair up all remaining negative signs by 'moving' them and eliminate them
        #   3. If there are no negative signs remaining, return the sum of the matrix
        #   4. Otherwise, move the negative sign to the smallest magnitude number
        #   5. Return the sum of the matrix

        # This can be short circuited by just
        #   1. Counting the number of negative signs
        #   2. If even, return the absolute sum of the matrix
        #   3. If odd, return the absolute sum of the matrix bar of everything but the smallest element 
        #       and subtract the absolute value of the smallest element
        min_abs_value = abs(matrix[0][0])           # 2
        negative_sign_count = 0                     # 1
        abs_sum = 0                                 # 14
        has_zero = False
        for i, row in enumerate(matrix):
            for j, value in enumerate(row):
                if value < 0:
                    negative_sign_count += 1
                if value == 0:
                    has_zero = True
                abs_value = abs(value)
                abs_sum += abs_value
                if abs_value < min_abs_value:
                    min_abs_value = abs_value
        
        return abs_sum if has_zero or negative_sign_count % 2 == 0 else abs_sum - 2 * min_abs_value

"""
[5, 2]
[3, -4]

"""

693