USACO Number Triangles

Code

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
27
28
29
30
31
32
33
34
35
/*
ID: wcr19961
PROG: numtri
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <queue>
using namespace std;

const int maxn = 1010;
int dp[maxn][maxn];

int main() {
freopen("numtri.in", "r", stdin);
freopen("numtri.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
int x;
scanf("%d", &x);
dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j]) + x;
}
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
ans = max(ans, dp[n][i]);
}
printf("%d\n", ans);
return 0;
}