#K78862. Maximum Score Difference

    ID: 35181 Type: Default 1000ms 256MiB

Maximum Score Difference

Maximum Score Difference

You are given a list of integers representing the scores of players in the order of their rankings. The task is to determine the maximum score difference between any two players, where the player with the lower score must have a higher ranking (i.e., appear earlier in the list) than the player with the higher score. If no such pair exists (i.e., the scores are in non‐increasing order), output -1.

More formally, given a sequence (a_1, a_2, \dots, a_n), find the maximum value of (a_j - a_i) for any (i < j) such that (a_j > a_i). If no valid pair exists, return -1.

Example: For the input [6, 1, 9, 5, 7], the maximum difference is (9 - 1 = 8).

inputFormat

The first line contains an integer (n) ( (n \geq 2)) representing the number of players. The second line contains (n) space-separated integers representing the scores of the players in the order of their rankings (from highest to lowest rank).

outputFormat

Output a single integer which is the maximum score difference. If there is no pair of players where the later has a higher score than an earlier one, output -1.## sample

5
6 1 9 5 7
8