SDE Path

Minimum Path Sum in a Grid

Medium

Minimum Path Sum in a Grid

Find the path from top-left to bottom-right of a grid that minimizes the sum of values along the way.

Input format

The first line contains R and C. The next R lines contain grid values (0–50).

Output format

Print the minimum path sum from top-left to bottom-right.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
6 4
5 32 43 47
44 3 11 9
13 27 45 43
8 42 10 10
32 20 34 50
34 33 43 48
Expected output
48
Example 2
Input
1 3
44 46 1
Expected output
1