AcWing 898. 数字三角形
原题链接
简单
作者:
自豪的澡巾QAQ
,
2021-02-15 13:44:50
,
所有人可见
,
阅读 266
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 505;
int num[N][N],dp[N][N];
int main() {
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
for (int j = 1;j <= i;j++) {
cin >> num[i][j];
}
}
for (int i = 0;i <= n;i++) {
for (int j = 0;j <= i + 1;j++) {
dp[i][j] = -1e9;
}
}
dp[1][1] = num[1][1];
for (int i = 2;i <= n;i++) {
for (int j = 1;j <= i;j++) {
dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - 1]) + num[i][j];
}
}
int ans = -100000;
for (int i = 1;i <= n;i++) {
ans = max(dp[n][i],ans);
}
cout << ans << endl;
}