fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Main
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc=new Scanner(System.in);
  13. int t=sc.nextInt();
  14. while(t-->0){
  15. int n=sc.nextInt();
  16. int mat[][]=new int[n][n];
  17. for(int i=0;i<n;i++){
  18. for(int j=0;j<n;j++){
  19. mat[i][j]=sc.nextInt();
  20. }
  21. }
  22. int sub[]=new int[2*n-1];
  23. for(int i=0;i<n;i++){
  24. for(int j=0;j<n;j++){
  25. sub[i-j+n-1]=Math.min(sub[i-j+n-1],mat[i][j]);
  26. }
  27. }
  28. int ans=0;
  29. for(int i=0;i<n;i++){
  30. if(sub[i]<0){
  31. ans-=sub[i];
  32. }
  33. }
  34. System.out.println(ans);
  35. }
  36.  
  37. }
  38. }
Success #stdin #stdout 0.14s 56572KB
stdin
4
1
1
2
-1 2
3 0
3
1 2 3
-2 1 -1
0 0 -1
5
1 1 -1 -1 3
-3 1 4 4 -4
-1 -1 3 0 -5
4 5 3 -3 -1
3 1 -3 -1 5
stdout
0
1
2
13