fork download
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.io.*;
  4.  
  5.  
  6. class SumInTriangle {
  7. public static void main(String args[]) throws IOException{
  8. int[][] arr=new int[100][100];
  9.  
  10. int testcases =Integer.parseInt(br.readLine());
  11.  
  12. while(testcases>0)
  13. {
  14.  
  15. int n = Integer.parseInt(br.readLine());
  16. for(int i=0;i<n;i++)
  17. {
  18. String input=br.readLine();
  19. String s[]=input.split(" ");
  20. for(int j=0;j<=i;j++)
  21. arr[i][j]= Integer.parseInt(s[j]);
  22. }
  23. for(int i=n-2;i>=0;i--)
  24. {
  25. for(int j=0;j<=i;j++)
  26. {
  27. arr[i][j]+=arr[i+1][j]>arr[i+1][j+1]?arr[i+1][j]:arr[i+1][j+1];
  28. }
  29. }
  30. System.out.printf("%d\n",arr[0][0]);
  31. testcases--;
  32. }
  33.  
  34.  
  35. }
  36.  
  37. }
Success #stdin #stdout 0.07s 381248KB
stdin
2
3
1
2 1
1 2 3
4
1
1 2
4 1 2
2 3 1 1
stdout
5
9