#K52382. Maximum Difference Between Array Elements

    ID: 29297 Type: Default 1000ms 256MiB

Maximum Difference Between Array Elements

Maximum Difference Between Array Elements

Given an array of integers, your task is to compute the maximum difference between any two elements such that the larger element appears after the smaller one. Formally, given an array \(A\) of length \(n\), find the maximum value of \(A[j] - A[i]\) for which \(0 \leq i < j < n\). If no such pair exists (for example, if the array is empty or contains a single element, or if the array is in non-increasing order), the answer is 0.

For example, in the array [2, 3, 10, 6, 4, 8, 1] the maximum difference is 8 (i.e. 10 - 2). Your solution should read the input from standard input (stdin) and write the result to standard output (stdout).

inputFormat

The first line of the input contains an integer \(n\) (\(n \geq 0\)), the number of elements. The second line contains \(n\) space-separated integers representing the array elements.

outputFormat

Output a single integer: the maximum difference as defined above. If no valid pair exists, output 0.

## sample
7
2 3 10 6 4 8 1
8