#K15746. Minimum Cost Path

    ID: 24425 Type: Default 1000ms 256MiB

Minimum Cost Path

Minimum Cost Path

You are given a matrix of positive integers where each element represents the cost of passing through that cell. Your task is to find the minimum cost path from the top-left corner to the bottom-right corner of the matrix. You are only allowed to move right or down at any step.

The cost of a path is defined as the sum of the costs of all the cells that form the path.

The recurrence relation for calculating the minimum cost is given by:

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

Make sure to handle edge cases where the matrix has a single row, a single column, or even a single element.

inputFormat

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

  • The first line contains two integers m and n, which represent the number of rows and columns in the matrix, respectively.
  • The next m lines each contain n space-separated integers, which represent the cost values of the matrix.

outputFormat

Output a single integer to stdout representing the minimum cost to traverse from the top-left corner to the bottom-right corner of the matrix.

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

</p>