#K16321. Minimum Cost Path in a Grid

    ID: 24553 Type: Default 1000ms 256MiB

Minimum Cost Path in a Grid

Minimum Cost Path in a Grid

You are given a grid of integers where each cell represents the cost to step into that cell. Your task is to find the minimum cost required to travel from the top-left corner (start) to the bottom-right corner (destination) of the grid.

You can only move either down or right at any step. The cost of a path is the sum of the costs of all the cells visited, including the starting and ending cells.

The dynamic programming recurrence used is given by the formula: \(dp[i][j] = \min(dp[i-1][j], dp[i][j-1]) + cost[i][j]\) where \(dp[i][j]\) represents the minimum cost to reach cell \((i, j)\).

inputFormat

The first line contains two integers \(n\) and \(m\) representing the number of rows and columns in the grid, respectively. Each of the next \(n\) lines contains \(m\) space-separated integers representing the cost values of the grid.

outputFormat

Output a single integer which is the minimum cost to reach the bottom-right corner from the top-left corner.

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