#C7032. Maximum Difference in an Array

    ID: 50859 Type: Default 1000ms 256MiB

Maximum Difference in an Array

Maximum Difference in an Array

You are given an integer n and a list of n integers. Your task is to compute the maximum difference d between any two elements in the list such that the larger element comes after the smaller one. In other words, if the list is denoted as \(a_1, a_2, \dots, a_n\), find the maximum value of \(a_j - a_i\) for \(1 \le i < j \le n\). If no such pair exists (i.e. the array is non-increasing or contains only one element), output -1.

The solution needs to read from standard input and output the answer to standard output.

inputFormat

The first line of input contains a single integer n representing the number of elements in the array. The second line contains n space-separated integers representing the elements of the array.

Example:

7
2 3 10 2 4 8 1

outputFormat

Output a single integer, which is the maximum difference \(d = a_j - a_i\) under the condition that \(i < j\). If no valid pair exists, output -1.

Example:

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