fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(){
  5. int n,m;
  6. cin>>n>>m;
  7.  
  8. vector<vector<int>> v(n, vector<int>(m));
  9. long long x = 0;
  10. int cnt =0;
  11. int minn = INT_MAX;
  12. // vector<vector<int>> matrix(rows, vector<int>(cols)); // 2D vector initialization
  13.  
  14. for(int i =0;i<n;i++){
  15. for(int j=0;j<m;j++){
  16. cin>>v[i][j];
  17.  
  18. x=x+abs(v[i][j]);
  19. if(v[i][j]<0) cnt++;// count all the negatives
  20.  
  21. minn = min(minn, abs(v[i][j]));
  22.  
  23. }
  24. }
  25.  
  26. if(cnt%2 == 0){
  27. cout<<x<<endl;
  28. }else{
  29. cout<<x -(2*minn)<<endl;
  30. }
  31. }
  32.  
  33.  
  34.  
  35.  
  36. int main() {
  37. int t;cin>>t;
  38. while(t--){
  39. solve();
  40. }
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5308KB
stdin
5
2 2
-1 1
1 1
3 4
0 -1 -2 -3
-1 -2 -3 -4
-2 -3 -4 -5
2 3
27 -66 -13
52 -56 -40
7 3
-6 1 -4
-84 -39 12
-55 34 -77
54 -100 -3
-58 50 37
16 68 78
-38 -36 87
4 2
-50 -21
-16 -7
86 76
62 -43
stdout
2
30
254
935
347