Best Time to Buy and Sell Stock

EasyArrays
Next
ArrayGreedy

You are given the price of a stock on each day. Choose one day to buy and a later day to sell to maximise profit. If no profit is possible, the answer is 0.

Input

Line 1: n, the number of days. Line 2: n prices.

Output

Print the maximum profit.

Constraints

1 <= n <= 10^5 0 <= price <= 10^9

Example 1
Input
6
7 1 5 3 6 4
Output
5

Buy at 1, sell at 6 for a profit of 5.

Example 2
Input
5
7 6 4 3 1
Output
0

Prices only fall, so do not trade.

Hints

Best Time to Buy and Sell Stock - Easy Arrays Problem | Skill Shiksha