#K73582. Maximum Elevation Difference

    ID: 34007 Type: Default 1000ms 256MiB

Maximum Elevation Difference

Maximum Elevation Difference

You are given an \( n \times m \) grid where each cell represents the elevation of that point. Starting from the top-left corner and moving only right or down, your task is to find a path to the bottom-right corner such that the difference between the highest and lowest elevations encountered on the path is maximized.

More formally, if the path visits cells with elevations \( e_1, e_2, \dots, e_k \), you must compute the value:

\( \max(e_1, e_2, \dots, e_k) - \min(e_1, e_2, \dots, e_k) \)

This problem can be solved using dynamic programming by maintaining, for each cell, the minimum and maximum elevation seen along a valid path from the start to that cell.

inputFormat

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

  • The first line contains two integers \( n \) and \( m \) representing the number of rows and columns.
  • The next \( n \) lines each contain \( m \) integers separated by spaces, representing the elevation values of each row of the grid.

outputFormat

Output a single integer to standard output (stdout), which is the maximum possible elevation difference along a valid path from the top-left corner to the bottom-right corner.

## sample
3 3
1 3 5
6 7 8
4 6 10
9

</p>