#C14927. Maximum Difference

    ID: 44630 Type: Default 1000ms 256MiB

Maximum Difference

Maximum Difference

Given an array of integers, your task is to compute the maximum difference between two elements such that the larger element comes after the smaller one. Formally, for an array \(A[0 \dots n-1]\), you are required to find the value of \(\max_{0 \le i < j < n}(A[j]-A[i])\). If the array is non-increasing (i.e., no element with a larger following element exists), then the answer should be 0.

This problem is a classic example of a one-pass optimization challenge where tracking the minimum value so far allows a solution in \(O(n)\) time.

Note: All input should be read from standard input and output should be printed to standard output.

inputFormat

The first line contains an integer \(n\), representing the number of elements in the array. The second line contains \(n\) space-separated integers which form the array \(A\).

outputFormat

Print a single integer which is the maximum difference computed as described above. If no valid pair exists, output 0.

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