ExpertAlgorithms50 points

Best day to buy and sell

A small trader tracks the daily price of a commodity. She can buy once and sell once later (the sale must be on a day after the purchase). She wants to know the best profit she could have made.

Task: the first line holds n, the second line holds n prices separated by spaces. Print the maximum profit. If no profit is possible, print 0.

Sample test

Input

6
7 1 5 3 6 4

Expected output

5

Your answer is also checked against 3 hidden tests.

Need a hint?

Walk through the prices once, remembering the lowest price so far and the best profit so far.