Skip to content

Files

Latest commit

Aug 16, 2024
1e1f1d8 · Aug 16, 2024

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Feb 16, 2024
Aug 16, 2024
Feb 8, 2024
Feb 16, 2024
Aug 16, 2024

Maximum Path


Given is a triangle of numbers. A path through the triangle is a sequence of adjacent numbers, one from each row, starting from the top. For example, 1 3 3 4 1 0 is a path in the following triangle of numbers:

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

Your task is to find the maximum path cost, where the path cost is defined as the sum of the numbers in the path. Thus, the given example has a maximum path cost of 1 + 5 + 2 + 9 + 7 + 8 = 32 , as shown below:

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

Write a program that reads a triangle of numbers and outputs the maximum path cost. The first line of the input contains the number of rows. Each subsequent line represents a row of the triangle, starting with the top row. Within each row, the numbers are separated by a space. The triangle has at most 1000 rows. All numbers are positive integers between 0 and 100.


Example

Input:

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

Output:

32