Skip to lesson

A medium problem for practising 5.4 Array & String Techniques. Solve it in your own IDE, check it against every test, and open a hint only when you are stuck. 0/1 solved.

1. Best single-trade profit

medium

A student follows one share price for n days in a paper-trading app. The price is in paise. She may buy one share on one day and sell it on a later day, at most once.

Print the largest profit she can make. If every possible trade loses money or gains nothing, she does not trade, so print 0.

Input: The first line has one integer n. The second line has n integers: the price on each day, in order.

Output: One integer: the largest profit, or 0.

Limits: 1 ≤ n ≤ 10^5 · 1 ≤ price ≤ 10^12

Example 1

Input

6
7 1 5 3 6 4

Output

5

Buy on day 2 at 1 and sell on day 5 at 6, for a profit of 5.

Example 2

Input

4
9 7 5 3

Output

0

The price only falls, so the best choice is not to trade.

Submit to solve