LogiQuest

230. Triangle Minimum Path

MediumDynamic Programming

From the top of the number triangle, move to an adjacent number on the row below each step. Print the minimum path sum to the bottom.

Input format

Line 1: n (rows) Next n lines: row i has i numbers

Output format

The minimum path sum.

Example 1

Input

4
2
3 4
6 5 7
4 1 8 3

Output

11

Example 2

Input

1
-10

Output

-10

Constraints

1 ≤ n ≤ 1000

Read input from standard input and print the answer to standard output. Your code is judged against 3 test cases, most of them hidden.

Loading the MyLoginn IDE…