#K89197. Minimum Cost Path in a Grid

    ID: 37477 Type: Default 1000ms 256MiB

Minimum Cost Path in a Grid

Minimum Cost Path in a Grid

You are given an n x n grid where each cell contains a positive integer representing the cost to step on that cell. Your task is to determine the minimum cost path from the top-left cell (cell (1,1)) to the bottom-right cell (cell (n,n)). You can only move either right or down at any step.

The cost of a path is the sum of the values in all the visited cells. Formally, if \(grid[i][j]\) represents the cost at cell \((i,j)\), you need to compute:

\[ \min_{\text{paths}} \sum_{(i,j)\in \text{path}} grid[i][j] \]

Print the minimum path cost.

inputFormat

The input is read from standard input and is formatted as follows:

  • The first line contains an integer \( n \), the size of the grid.
  • The next \( n \) lines each contain \( n \) space-separated integers representing the grid.

outputFormat

Output a single integer representing the minimum cost to travel from the top-left cell to the bottom-right cell.

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

</p>