题目描述 Description
如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得向右走,一直走到底层,要求找出一条路径,使路径上的值最大。

输入描述 Input Description
第一行是数塔层数N(1<=N<=100)。
第二行起,按数塔图形,有一个或多个的整数,表示该层节点的值,共有N行。
第二行起,按数塔图形,有一个或多个的整数,表示该层节点的值,共有N行。
输出描述 Output Description
输出最大值。
样例输入 Sample Input
5
13
11 8
12 7 26
6 14 15 8
12 7 13 24 11
13
11 8
12 7 26
6 14 15 8
12 7 13 24 11
样例输出 Sample Output
86
显然dp
显然dp
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1000][1000];
int f[1000][1000];
int main()
{
int n;
cin>>n;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= i;j ++)
cin>>a[i][j];
}
for(int i = n;i >= 1;i --)
{
for(int j = i;j >= 1;j --)
{
f[i][j] = a[i][j] + max(f[i + 1][j], f[i + 1][j + 1]);
}
}
cout<<f[1][1];
return 0;
}