1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <bits/stdc++.h> using namespace std; const int N = 510, INF = 1e9; int n; int a[N][N], f[N][N];
int main() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) cin >> a[i][j];
for (int i = 0; i <= N; i++) for (int j = 0; j <= N; j++) f[i][j] = -INF;
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]);
int res = -INF; for (int i = 1; i <= n; i++) res = max(res, f[n][i]); cout << res << endl; return 0; }
|