# Codeforces 867 E. Buy Low Sell High （贪心）

## Description

You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don’t own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.

## Input

Input begins with an integer N (2 ≤ N ≤ 3·10^5), the number of days.

Following this is a line with exactly N integers p1, p2, …, pN (1 ≤ pi ≤ 10^6). The price of one share of stock on the i-th day is given by pi.

## Output

Print the maximum amount of money you can end up with at the end of N days.

## Examples input

9
10 5 4 7 9 12 6 2 10


## Examples output

20


## AC 代码

#include<bits/stdc++.h>
using namespace std;
typedef __int64 LL;

multiset<int> sk;
int main()
{
int n;
LL ans = 0;
cin>>n;
for(int i=0; i<n; i++)
{
int x;
cin>>x;
if(!sk.empty()&&*sk.begin()<x)
{
ans+=x-*sk.begin();
sk.erase(sk.begin());
sk.insert(x);
sk.insert(x);
}
else
sk.insert(x);
}
cout<<ans<<endl;
return 0;
}