标题:数字三角形
我的代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n, w[N][N], f[N][N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cin >> w[i][j];
}
}
for (int j = 1; j <= n; j++) {
f[n][j] = w[n][j];
}
for (int i = n - 1; i >= 0; i-- )
for (int j = 1; j <= i; j++ )
f[i][j] = max(f[i + 1][j] + w[i][j], f[i + 1][j + 1] + w[i][j]);
cout << f[1][1] << endl;
return 0;
}