cswork / OAOpen practice workspace

Pure StorageOA #1

Maximum Historical Income

Estimated MediumJudge-ready

Topics (AI-labelled)Stock TradingGreedy

The price on day i is A[i]. You initially hold 1 unit of the asset and can hold at most 1 unit at any time; you may sell on any day and buy when you are not holding. Find the maximum total profit and output it modulo 10^9.

Input

The first line contains n; the second line contains n prices. 1≤n≤200000, 0≤A[i]≤10^9.

Output

Output the maximum total profit mod 1000000000.

Example 1

Input:

7
1 2 3 3 2 1 5

Output:

7

Example 2

Input:

3
5 1 1

Output:

5

Example 3

Input:

4
0 1000000000 0 1000000000

Output:

0

Editorial

Loading solution…