CF865D.Buy Low Sell High

传统题 时间 2000 ms 内存 256 MiB 8 尝试 12 已通过 8 标签

Buy Low Sell High

Buy Low Sell High

Statement

You can perfectly predict the price of a certain stock for the next N days. You want to make as much profit as possible, but on each day you may transact at most one share: buy one share, sell one share, or do nothing.

Initially, you own zero shares and you cannot sell a share that you do not own. At the end of day N, you must again own zero shares.

Find the maximum possible profit.

Input

The first line contains one integer N (2 <= N <= 3 * 10^5), the number of days.

The second line contains N integers p_1, p_2, ..., p_N (1 <= p_i <= 10^6), where p_i is the price of one share on day i.

Output

Print one integer, the maximum amount of money that can be earned.

Samples

Sample 1

Input:

9
10 5 4 7 9 12 6 2 10

Output:

20

Sample 2

Input:

20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4

Output:

41

在线编程 IDE

建议全屏模式获得最佳体验