#C9138. Minimum Energy Path

    ID: 53198 Type: Default 1000ms 256MiB

Minimum Energy Path

Minimum Energy Path

You are given a grid of positive integers representing energy values. Your task is to determine the minimum energy required to travel from the top-left corner to the bottom-right corner of the grid. You can only move either to the right or down at any step.

Let \( grid[i][j] \) be the energy at cell \( (i, j) \). The recurrence relation to compute the minimum energy is given by:

\[ \text{dp}[i][j] = \min(\text{dp}[i-1][j],\, \text{dp}[i][j-1]) + grid[i][j] \]

with the base case \( \text{dp}[0][0] = grid[0][0] \).

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains two integers \( R \) and \( C \) representing the number of rows and columns in the grid.
  • This is followed by \( R \) lines, each containing \( C \) space-separated integers representing the energy values of the grid cells.

outputFormat

Print a single integer to standard output (stdout), which is the minimum energy sum required to reach the bottom-right corner from the top-left corner of the grid.

## sample
3 4
5 9 1 3
4 7 2 6
8 5 3 2
22