Digital triangle
Description
Input
The first line of an integer N (<= 1000), a triangle represents the total of the second to a few lines + 1 line N, this number is given triangle
Output
An integer that represents the largest number of all of the way and the results will not exceed int64
Sample Input
4
1
3 2
4 10 1
4 3 2 20
Sample Output
24
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
long long f[1001][1001], a[1001][1001];
int n;
int main() {
scanf("%d", &n);
for (int i = 1;i <= n;i ++) {
for (int j = 1;j <= i;j ++) {
scanf("%lld", &a[i][j]);
}
}
memset(f, 0, sizeof(f));
for (int i = 1;i <= n;i ++) {
for (int j = 1;j <= i;j ++) {
if (f[i - 1][j] > f[i - 1][j - 1]) {
f[i][j] = f[i - 1][j] + a[i][j];
}
else {
f[i][j] = f[i - 1][j - 1] + a[i][j];
}
}
}
long long ans = 0;
for (int i = 1; i <= n; i ++) {
if (ans < f[n][i]) {
ans = f[n][i];
}
}
cout << ans << endl;
return 0;
}