#K84302. Minimum Path Cost

    ID: 36389 Type: Default 1000ms 256MiB

Minimum Path Cost

Minimum Path Cost

You are given a 2D grid of integers. Your task is to find the minimum cost to travel from the top-left corner of the grid to the bottom-right corner. You can only move either right or down at any step.

The cost of a path is the sum of all the integers in the cells that you visit, including the starting and ending cells. Use dynamic programming to solve the problem efficiently.

Note: All formulas below are in LaTeX format. The recurrence for the dynamic programming solution can be expressed as:

\[ dp[i][j] = grid[i][j] + \min\begin{cases} dp[i-1][j] & \text{if } i > 0, \\ dp[i][j-1] & \text{if } j > 0. \end{cases} \]

inputFormat

The first line contains two integers m and n separated by a space, representing the number of rows and columns in the grid respectively.

The next m lines contain n integers each, separated by spaces, representing the grid.

outputFormat

Output a single integer, the minimum cost to traverse from the top-left to the bottom-right cell of the grid.

## sample
3 3
1 3 1
1 5 1
4 2 1
7