AcWing 3304. 数字三角形
原题链接
简单
作者:
stc
,
2024-03-04 01:05:22
,
所有人可见
,
阅读 24
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int a[N][N];
int f[N][N];
// f[i][j]表示从(1,1)走到(i,j)的最大和
// f[i][j] = max(f[i-1][j-1]+a[i][j],f[i-1][j]+a[i][j]);
int main()
{
cin >> n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cin >> a[i][j];
}
}
f[1][1] = a[1][1];
for(int i=2;i<=n;i++){
for(int j=1;j<=i;j++){
f[i][j] = max(f[i-1][j-1]+a[i][j],f[i-1][j]+a[i][j]);
}
}
if(n&1) cout << f[n][n/2+1] << endl; // n是奇数,则一定是正中间
else cout << max(f[n][n/2],f[n][n/2+1]) << endl; // n是偶数,则一定是中间两个
return 0;
}