1975.cpp (506B)
1 class Solution { 2 public: 3 long long maxMatrixSum(const vector<vector<int>> &matrix) const { 4 const int n = size(matrix); 5 int count = 0, mini = INT_MAX; 6 long long res = 0; 7 8 for (int i = 0; i < n; i++) { 9 for (int j = 0; j < n; j++) { 10 res += abs(matrix[i][j]); 11 count += matrix[i][j] < 0; 12 mini = min(mini, abs(matrix[i][j])); 13 } 14 } 15 16 return count % 2 == 0 ? res : res - 2 * mini; 17 } 18 };