AcWing 898. [Java 一维数组] 数字三角形
原题链接
简单
作者:
Aranne
,
2020-07-08 06:38:46
,
所有人可见
,
阅读 731
import java.util.*;
class Main {
Scanner sc = new Scanner(System.in);
int n;
int maxn = 505;
int[] dp = new int[maxn];
void run() {
n = sc.nextInt();
Arrays.fill(dp, Integer.MIN_VALUE);
dp[1] = sc.nextInt();
for (int i = 2; i <= n; i++) {
for (int j = i; j >= 1; j--) { //从右到左
dp[j] = Math.max(dp[j-1], dp[j]) + sc.nextInt();
}
}
int res = Integer.MIN_VALUE;
for (int i = 1; i <= n; i++) {
res = Math.max(res, dp[i]);
}
System.out.println(res);
}
public static void main(String[] args) {
Main m = new Main();
m.run();
}
}