The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 

          7


3 8

8 1 0

2 7 4 4

4 5 2 6 5
Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. 

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N 

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int data[351][351];
int dp[351];
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=i;j++)
            cin>>data[i][j];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        for(int j=i;j>=1;j--){//这里为何j要逆序开始算呢?等号右边的dp数组是对应的i-1,如果升学计算的话,等号左边被赋值之后,也就意味着i-1数组被修改了,而下次计算正好需要这次修改的原来的数组的值
            dp[j]=max(dp[j-1],dp[j])+data[i][j];
        }
    }
    int m=dp[1];
    for(int i=2;i<=n;i++){
        if(dp[i]>m)
            m=dp[i];
    }
    cout<<m;
    return 0;
}

 

 

 #include<iostream>
 #include<string.h>
 using namespace std;
 int way[MAXN],dp[MAXN];
 int N;
 void Input()
 {
     cin>>N;
     memset(dp,0,sizeof(dp));
     for (int i=1; i<=N; i++)
     {
         for (int j=1; j<=i; j++)
             scanf("%d",&way[j]);
         for (int j=i; j>=1; j--)
             dp[j]=max(dp[j],dp[j-1])+way[j];
     }
 }
 void Output()
 {
     int ret=dp[1];
     for (int i=2; i<=N; i++)
         if (ret<dp[i]) ret=dp[i];
     cout<<ret<<endl;
 }
 int main()
 {
     Input();
     Output();
     return 0;
 }