Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/minmax-36-eng-foydali-savdo

Most profitable deal

Medium 1000 ms 128 MB 45%
Share

The prices of a product for $N$ days are given: $A_{1}, A_{2}, \ldots, A_{N}$. The product is bought on day $i$ and sold later, on day $j$ ($i \lt j$); the profit is:

$$A_{j} - A_{i}$$

Find the largest profit. Buying and selling are mandatory, so the profit may also be negative.

In the sequence of prices, first the cheapest and then the most expensive day is chosen

Input

The first line contains a positive integer $N$. The next line contains $N$ integers $A_{i}$.

Output

One integer — the largest profit.

Note

$2 \le N \le 10^{5}$
$1 \le A_{i} \le 10^{9}$

Examples

Input #1
6
7 2 5 1 6 3
Output #1
5
Input #2
3
5 4 2
Output #2
-1

Sign in to submit a solution

Signing up is free and takes a minute.