Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static int dp[][];
- static int tri[][];
- static int n;
- static int max;
- @SuppressWarnings("resource")
- public static void main(String args[]) {
- Scanner sc = new Scanner(System.in);
- n = sc.nextInt();
- tri = new int[n + 1][n + 1];
- dp = new int[n + 1][n + 1];
- for (int i = 1; i < n + 1; i++) {
- for (int j = 1; j < i + 1; j++) {
- tri[i][j] = sc.nextInt();
- dp[i][j] = tri[i][j];
- }
- }
- run();
- System.out.println(max);
- }
- static void run() {
- for (int i = 1; i < n + 1; i++) {
- for (int j = 1; j < n + 1; j++) {
- if (dp[i - 1][j - 1] > dp[i - 1][j]) {
- dp[i][j] += dp[i - 1][j - 1];
- } else {
- dp[i][j] += dp[i - 1][j];
- }
- if (max < dp[i][j]) {
- max = dp[i][j];
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment