#C3238. Minimum Cost Path in a Grid

    ID: 46643 Type: Default 1000ms 256MiB

Minimum Cost Path in a Grid

Minimum Cost Path in a Grid

You are given a grid of size \(n \times m\) where each cell contains a non-negative integer representing the cost of visiting that cell. Starting from the top-left corner, your goal is to reach the bottom-right corner by only moving either right or down. The total cost of a path is the sum of the costs of all cells visited along the path. Your task is to compute the minimum cost required to reach the bottom-right cell.

Input Format: The first line contains two integers \(n\) and \(m\), representing the number of rows and columns respectively. This is followed by \(n\) lines, each containing \(m\) space-separated integers representing the grid.

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

Example:

Input:
3 3
1 3 1
1 5 1
4 2 1

Output: 7

</p>

inputFormat

The input is received from standard input (stdin). It consists of:

  1. A line with two integers \(n\) and \(m\), which denote the number of rows and columns in the grid.
  2. \(n\) subsequent lines, each containing \(m\) space-separated integers representing the cost grid.

outputFormat

Output a single integer to standard output (stdout), representing the minimum cost path from the top-left to the bottom-right of the grid.

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