#C9270. Maximum Difference Problem

    ID: 53345 Type: Default 1000ms 256MiB

Maximum Difference Problem

Maximum Difference Problem

Given a sequence of integers, your task is to compute the maximum difference between a pair of numbers such that the larger number comes after the smaller number. Formally, given an array \(a_0, a_1, \dots, a_{n-1}\), find:

\(\max_{0 \le i < j < n} (a_j - a_i)\)

If no valid pair exists (for example, when the list contains fewer than two elements or all differences are negative or zero), output 0.

inputFormat

The input is given via standard input. The first line contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers describing the array.

outputFormat

Output a single integer which is the maximum difference \(a_j - a_i\) such that \(i < j\) and \(a_j \ge a_i\). If no such pair exists, output 0.

## sample
6
7 1 5 3 6 4
5