LogiQuest

229. Minimum Path Sum

MediumDynamic ProgrammingGrid

Moving only right or down, print the minimum sum of a path from the top-left to the bottom-right of the grid.

Input format

Line 1: r c Next r lines: c non-negative integers

Output format

The minimum sum.

Example 1

Input

3 3
1 3 1
1 5 1
4 2 1

Output

7

Example 2

Input

2 3
1 2 3
4 5 6

Output

12

Constraints

1 ≤ r, c ≤ 500

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

Loading the MyLoginn IDE…