#C9546. Maximum Elevation Gain

    ID: 53651 Type: Default 1000ms 256MiB

Maximum Elevation Gain

Maximum Elevation Gain

You are given a series of elevation points along a hiking route. Your task is to compute the maximum elevation gain, defined as the greatest difference between two elevations where the lower elevation occurs before the higher elevation. If no gain is possible, output 0.

Mathematically, given an array \( a_1, a_2, \dots, a_n \), find the maximum value of \( a_j - a_i \) for all \( i < j \). If no such pair exists (i.e. the sequence is non-increasing), output 0.

inputFormat

The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer ( n ) (the number of elevation points). The second line contains ( n ) space-separated integers representing the elevations.

outputFormat

Output a single integer to standard output (stdout), which is the maximum elevation gain.## sample

5
2 3 1 5 4
4