898 数字三角形
作者:
jy9
,
2024-08-06 13:36:21
,
所有人可见
,
阅读 1
开始接触dp啦,希望越来越好
#include <iostream>
#include <cstring>
using namespace std;
const int N = 510;
const int INF = 0x3f3f3f3f;
int dp[N][N];
int a[N][N];
int main(){
int n;
cin >> n;
memset(dp, -INF, sizeof dp);
for (int i = 1; i <= n; i ++ ){
for (int j = 1; j <= i; j ++ ){
cin >> a[i][j];
}
}
dp[1][1] = a[1][1];
for(int i = 2; i <= n; i++){
for (int j = 1; j <= i; j ++ ){
dp[i][j] = a[i][j] + max(dp[i-1][j], dp[i-1][j-1]);
}
}
int res = -INF;
for (int i = 1; i <= n; i ++ ) res = max(res, dp[n][i]);
cout << res;
return 0;
}