LogiQuest

36. Best Time to Buy and Sell Stock

EasyArrayGreedy

Given daily stock prices, buy on one day and sell on a later day. Print the maximum profit, or 0 if no profit is possible.

Input format

Line 1: n Line 2: n prices

Output format

The maximum profit.

Example 1

Input

6
7 1 5 3 6 4

Output

5

Example 2

Input

5
7 6 4 3 1

Output

0

Constraints

1 ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…