fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Main
  6. {
  7. public static void main (String[] args)
  8. {
  9. Scanner in = new Scanner(System.in);
  10. int[][] s = new int[1001][1001];
  11. while (in.hasNextInt())
  12. {
  13. int n = in.nextInt();
  14. int ans = 0;
  15. if(n==0)
  16. {
  17. break;
  18. } else {
  19. for (int i = 1; i <= n; i++)
  20. {
  21. for (int j = 1; j <= i; j++)
  22. {
  23. s[i][j] = in.nextInt();
  24. s[i][j] += s[i - 1][j];
  25. }
  26. }
  27. for (int j = 1; j <= n; j++)
  28. {
  29. for (int i = j; i <= n; i++)
  30. {
  31. s[i][j] += s[i - 1][j - 1];
  32. }
  33. for (int i = n - 1; i >= j; i--) {
  34. s[i][j] = Math.max(s[i][j], s[i + 1][j]);
  35. }
  36. }
  37. for (int i = 1; i <= n; i++) {
  38. ans = Math.max(ans, s[i][i]);
  39. }
  40. System.out.println(ans);
  41. }
  42. }
  43. }
  44. }
Success #stdin #stdout 0.08s 45088KB
stdin
3
7
2 1 
3 4 6
2
5
9 2
1
2
0
stdout
23
16
2