#C3473. Minimum Energy Cost Path

    ID: 46904 Type: Default 1000ms 256MiB

Minimum Energy Cost Path

Minimum Energy Cost Path

You are given a grid of integers with n rows and m columns. You start at the top-left cell (position (0, 0)) and want to reach the bottom-right cell (position (n-1, m-1)). At each step, you may move either to the right or downward. The cost of moving from one cell to an adjacent cell is given by the absolute difference of their values.

Formally, if you move from cell \(a\) to cell \(b\), the cost incurred is \(|a - b|\). The goal is to find a path from the top-left to the bottom-right cell with the minimum total energy cost.

Note: The starting cell does not contribute to the cost.

inputFormat

The first line contains two integers \(n\) and \(m\) — the number of rows and columns in the grid, respectively. Each of the next \(n\) lines contains \(m\) space-separated integers representing the grid.

outputFormat

Output a single integer representing the minimum total energy cost required to reach the bottom-right cell from the top-left cell.

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