#K76437. Minimum Cost Path in a Grid

    ID: 34642 Type: Default 1000ms 256MiB

Minimum Cost Path in a Grid

Minimum Cost Path in a Grid

You are given a grid with N rows and M columns. Each cell of the grid contains an integer representing the cost to enter that cell. Your task is to compute the minimum cost required to travel from the top-left corner (cell (0,0)) to the bottom-right corner (cell (N-1, M-1)). You are only allowed to move either right or down at any step.

The cost of a path is the sum of the costs of the cells visited along the path, including both the start and end cells. Formally, if you denote the grid as \(grid[i][j]\), you need to find a path \(P\) from \(grid[0][0]\) to \(grid[N-1][M-1]\) such that the total cost \[ \sum_{(i,j) \in P} grid[i][j] \] is minimized.

Note: Use standard input (stdin) for reading input and standard output (stdout) for printing your answer.

inputFormat

The input begins with a single line containing two space-separated integers \(N\) and \(M\), representing the number of rows and columns in the grid respectively. Following this, there are \(N\) lines; each line contains \(M\) space-separated integers representing the cost of each cell in that row.

Example:

3 4
1 3 5 8
4 2 1 7
4 3 2 3

outputFormat

Output a single integer which is the minimum cost to reach the bottom-right corner from the top-left corner by moving only right or down.

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

</p>