#K70872. Minimum Path Cost in a Grid

    ID: 33405 Type: Default 1000ms 256MiB

Minimum Path Cost in a Grid

Minimum Path Cost in a Grid

You are given a grid with N rows and M columns, where each cell contains a positive integer. Your task is to find the minimum cost to travel from the top-left corner (cell (0, 0)) to the bottom-right corner (cell (N-1, M-1)). You can only move either right or down.

The cost of a path is defined as the sum of the integers in each cell visited along the path. In other words, if you define a dynamic programming table \(dp\) where \[ dp[i][j] = \min(dp[i-1][j],\; dp[i][j-1]) + grid[i][j] \] with \(dp[0][0] = grid[0][0]\), then the answer will be \(dp[N-1][M-1]\).

Note: It is guaranteed that all grid numbers are positive.

inputFormat

The first line contains two space-separated integers N and M representing the number of rows and columns in the grid.

Each of the following N lines contains M space-separated integers, representing the grid rows.

outputFormat

Output a single integer, the minimum cost to reach the bottom-right corner of the grid.

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