C++ 代码
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 510;
int f[N][N];
int a[N][N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
cin >> a[i][j];
// 初始化
for (int i = 1; i <= n; i++)
f[n][i] = a[n][i];
// 状态转移
for (int i = n-1; i >=1; i--)
for (int j = i; j >= 1; j--)
f[i][j] = max(f[i+1][j], f[i+1][j+1]) + a[i][j];
cout << f[1][1] << endl;
return 0;
}