Problems > Cheapest Path Through the Grid

Cheapest Path Through the Grid

Difficulty: 1400 2000 ms 128 MB

dp

Walk from the top-left to the bottom-right cell moving only right or down, paying each visited cell's cost. Minimize the total.

Input: the first line has N and M; the next N lines each have M non-negative integers.

Output: the minimum path cost.

Constraints:

  • 1 ≤ N, M ≤ 1000
  • 0 ≤ cost ≤ 100

Sample Tests

Input 1
2 3
1 3 1
1 5 1
Output 1
6
Input 2
1 1
7
Output 2
7

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.