算法1 from bottom to top
(DP)
时间复杂度
O($n^2$)
C++ 代码
#include<iostream>
using namespace std;
const int N = 510;
int f[N][N];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
for(int j = 1; j <= i; ++j)
cin >> f[i][j];
for (int i = N - 1; i >= 1; i--) {
for (int j = 1; j <= i; ++j) {
f[i][j] += max(f[i+1][j], f[i+1][j+1]);
}
}
cout << f[1][1] << endl;
return 0;
}