Guest User

Untitled

a guest
Aug 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. static int dp[][];
  5. static int tri[][];
  6. static int n;
  7. static int max;
  8.  
  9. @SuppressWarnings("resource")
  10. public static void main(String args[]) {
  11. Scanner sc = new Scanner(System.in);
  12.  
  13. n = sc.nextInt();
  14. tri = new int[n + 1][n + 1];
  15. dp = new int[n + 1][n + 1];
  16.  
  17. for (int i = 1; i < n + 1; i++) {
  18. for (int j = 1; j < i + 1; j++) {
  19. tri[i][j] = sc.nextInt();
  20. dp[i][j] = tri[i][j];
  21. }
  22. }
  23.  
  24. run();
  25.  
  26. System.out.println(max);
  27. }
  28.  
  29. static void run() {
  30. for (int i = 1; i < n + 1; i++) {
  31. for (int j = 1; j < n + 1; j++) {
  32. if (dp[i - 1][j - 1] > dp[i - 1][j]) {
  33. dp[i][j] += dp[i - 1][j - 1];
  34. } else {
  35. dp[i][j] += dp[i - 1][j];
  36. }
  37. if (max < dp[i][j]) {
  38. max = dp[i][j];
  39. }
  40. }
  41. }
  42. }
  43. }
Add Comment
Please, Sign In to add comment