joj2511

 2511: Number triangle


Result
TIME Limit
MEMORY Limit
Run Times
AC Times
JUDGE
1s
65536K
427
217
Standard
        5
      3   4
    8   1   2
  5   4   3   6
2   1   7   9   8

A number trangle is composed of N(N<=100) line numbers, the i-th line contains i positive integers(<=100). A chess can walk from the top line of the triangle to the bottom line. Suppose the chess is on the k-th number of one line, then it can only move to the k-th number or the (k+1)-th number of the line below in one step. Find a path from the top to the bottom, which can maximize the sum of the integers on the path.

Input


There are multiple test cases.For each test case, there's an integer N representing the size of the triangle, followed by N lines of positive intergers, the first line has 1 integer, next has two integers... the Nth line has N integers.

Output


The maximum sum of the integers on the path.

Sample Input

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

Sample Output

16
12

Developed by skywind, SIYEE
#include #include using namespace std; int a[100+3][100+3]; int s[100+3][100+3]; int main() {     int n;     while(scanf("%d",&n)==1)     {         for(int i=1;i<=n;i++)         {             for(int j=1;j<=i;j++)             {                 scanf("%d",&a[i][j]);             }         }         s[1][1]=a[1][1];         for(int i=2;i<=n;i++)         {             s[i][1]=s[i-1][1]+a[i][1];             for(int j=2;j<=i;j++)             {                 s[i][j]=max(s[i-1][j],s[i-1][j-1])+a[i][j];             }         }         int maxt=-1;         for(int i=1;i<=n;i++)         {             if(s[n][i]>maxt)             {                 maxt=s[n][i];             }         }         printf("%d",maxt);     }     return 0; }
이것은 전형적인 동적 기획의 제목이다. 관건은 표현식 s[i][j]=max(s[i-1][j-1], s[i-1][j])+a[i][j]를 찾는 것이다.정점에서 i행 j 번째 점까지의 최대치를 나타냅니다...

좋은 웹페이지 즐겨찾기