Solution for waifujahat | C# | 100% Both using Simple Iteration
\n\n---\n\n# Step by Step \n1. The goal is I must maximize the sum of the elements in a matrix and I can flip the sign of adjacent elements of any two neighbori
![Screenshot 2024-11-24 194422.png](https://assets.leetcode.com/users/images/3f2ccdbd-2bf4-41ec-8daf-2dde83d235ab_1732452279.674894.png)\n\n---\n\n# Step by Step \n1. The goal is I must maximize the sum of the elements in a matrix and I can flip the sign of adjacent elements of any two neighboring elements in the matrix as needed, this\'s simple\n\n2. All I need to do is calculate the sum of absolute values of all elements, then count the negative numbers to determine if their signs can be balanced, I need to adjust the total sum if the count of negative numbers is odd\n\n3. I use nested `foreach` loops to traverse the matrix row by row and element by element, for each number, determine its absolute value in `value`, add the absolute value to `total` then update `smallestvalue` to track the smallest absolute value, if the number is negative, increment `negativecount`\n\n4. Check if the count of negative numbers `negativecount` is odd, if odd, subtract `2 * smallestvalue` from `total`, if even, no adjustment is needed and done\n\n---\n\n# Code\n```csharp []\npublic class Solution {\n    public long MaxMatrixSum(int[][] matrix) {\n\n        long total = 0;            \n        int smallestvalue = int.MaxValue; \n        int negativecount = 0;     \n\n        // Iterate over the matrix\n        foreach (var row in matrix) \n        {\n            foreach (int number in row) \n            {\n                int value; \n                \n                if (number < 0) \n                {\n                    // Convert negative number to positive\n                    value = -number;  \n                    negativecount++;   \n                } \n                else \n                {\n                    // Number is already positive\n                    value = number;   \n                }\n                total += value; \n                smallestvalue = Math.Min(smallestvalue, value); \n            }\n        }\n        // To check if the count of negatives is odd\n        if (negativecount % 2 == 1) \n        {\n            // I adjust total by removing the smallest absolute value twice\n            total -= 2 * smallestvalue;\n        }\n        return total;\n    }\n}\n```
['Array', 'Greedy', 'Matrix', 'C#']