Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/dp-sonlar-uchburchagi

Number triangle

Easy 1000 ms 128 MB 33%
Share

A number triangle with $n$ rows is given: row $i$ contains $i$ numbers. Starting from the top of the triangle, at every step you move down to one of the two adjacent numbers of the row below — straight down or down-right — until you reach the last row. What is the largest possible sum of the numbers on the path?

A number triangle: the path from the top to the bottom row with the largest sum is highlighted

Input

The first line contains a positive integer $n$. The $i$-th of the next $n$ lines contains $i$ integers.

Output

One integer — the largest sum.

Note

$1 \le n \le 500$
the numbers are from $-1000$ to $1000$

Examples

Input #1
4
7
3 8
8 1 0
2 7 4 4
Output #1
25
Input #2
1
-5
Output #2
-5

Sign in to submit a solution

Signing up is free and takes a minute.