AcWing 898. 数字三角形
原题链接
简单
作者:
dsf2
,
2021-03-04 08:11:33
,
所有人可见
,
阅读 259
import java.util.*;
class Main {
public static int n = 501;
public static void main(String[] args) {
int[][] triangle = new int[n][n];
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < i + 1; j++)
triangle[i][j] = sc.nextInt();
}
int[] dp = new int[n];
for (int r = rows - 1; r >= 0; r--) {
for (int i = 0; i < n - 1; i++)
dp[i] = Math.max(dp[i], dp[i + 1]) + triangle[r][i];
}
System.out.println(dp[0]);
}
}