#C9235. Minimum Cost Path in a Square Grid

    ID: 53306 Type: Default 1000ms 256MiB

Minimum Cost Path in a Square Grid

Minimum Cost Path in a Square Grid

You are given an n x n grid, where each cell contains a positive integer cost. Your task is to compute the minimum cost required to travel from the top-left corner to the bottom-right corner of the grid. You are only allowed to move either right or down at any step.

The problem can be formulated using dynamic programming. Define dp[i][j] as the minimum cost to reach cell (i,j). The recurrence relation is given by:

$$dp[i][j] = \min(dp[i-1][j],\; dp[i][j-1]) + grid[i][j] $$

with the base case dp[0][0] = grid[0][0].

Your solution should read the grid from standard input and output the minimum cost to standard output.

inputFormat

The first line contains an integer n, representing the size of the grid (number of rows and columns). The following n lines each contain n space-separated integers representing the cost values of the grid.

For example:

3
1 3 1
1 5 1
4 2 1

outputFormat

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

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

</p>