#C6446. Minimum Path Sum in a Matrix

    ID: 50207 Type: Default 1000ms 256MiB

Minimum Path Sum in a Matrix

Minimum Path Sum in a Matrix

You are given a matrix grid of non-negative integers with dimensions \(R \times C\). Each cell in the matrix represents the cost to traverse that cell. Your goal is to move from the top-left corner to the bottom-right corner of the matrix while incurring the minimum possible cost.

You can only move either down or right at any step. The task is to find and output the minimum path sum from the start to the destination.

If the grid is empty (that is, when either \(R=0\) or \(C=0\)), the output should be \(0\).

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two integers \(R\) and \(C\), representing the number of rows and columns of the grid respectively.
  • If both \(R > 0\) and \(C > 0\), the next \(R\) lines each contain \(C\) space-separated integers representing the grid's rows.

If either \(R=0\) or \(C=0\), the grid is considered empty and the answer is \(0\).

outputFormat

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

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