#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#define pb push_back
#define mp make_pair

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod pi=acos(-1);
const int oo=1<<30;
const LL OO=1e18;

const int N=1e6;

int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}

#define lc (i<<1)
#define rc (lc|1)
int tag[N];PR mi[N];
int a[N],b[N];
inline void build(int i,int l,int r) {
	mi[i]=mp(b[l],l);
	if (l!=r) {
		int mid=(l+r)>>1;
		build(lc,l,mid);
		build(rc,mid+1,r);
	}
}
inline void pushdown(int i) {
	if (tag[i]) {
		mi[lc].first+=tag[i],mi[rc].first+=tag[i];
		tag[lc]+=tag[i],tag[rc]+=tag[i];
		tag[i]=0;
	}
}
inline void modify(int i,int l,int r,int L,int R) {
	if (L<=l&&r<=R) tag[i]++,mi[i].first++;
	else {
		int mid=(l+r)>>1;
		pushdown(i);
		if (L<=mid) modify(lc,l,mid,L,R);
		if (mid<R) modify(rc,mid+1,r,L,R);
		mi[i]=min(mi[lc],mi[rc]);
	}
}
inline PR query(int i,int l,int r,int L,int R) {
	if (L<=l&&r<=R) return mi[i];
	int mid=(l+r)>>1;PR ans=mp(oo,0);
	pushdown(i);
	if (L<=mid) upmin(ans,query(lc,l,mid,L,R));
	if (mid<R) upmin(ans,query(rc,mid+1,r,L,R));
	return ans;
}
int main()
{
	int n=gi(),i,len,k;PR p;LL ans=0;
	for (i=1;i<=n;i++) a[i]=b[i]=gi();
	sort(b+1,b+1+n);
	len=unique(b+1,b+1+n)-b-1;
	build(1,1,len);
	for (i=1;i<=n;i++) {
		k=lower_bound(b+1,b+len+1,a[i])-b;
		p=query(1,1,len,k,len);
		ans+=p.first-a[i];
		if (p.second!=1)
			modify(1,1,len,1,p.second-1);
	}
	cout<<ans<<endl;
	return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

We have a sequence A of length N.

On this sequence, we can perform the following two kinds of operations:

* Swap two adjacent elements.

* Select one element, and increment it by 1.




We will repeatedly perform these operations so that A will be a non-decreasing sequence. Find the minimum required number of operations.

Constraints

* 1 ≤ N ≤ 200000
* 1 ≤ A_i ≤ 10^9
* A_i is an integer.

Input

Input is given from Standard Input in the following format:


N
A_1
A_2
:
A_N


Output

Print the minimum number of operations required to turn A into a non-decreasing sequence.

Examples

Input

5
4
1
8
8
7


Output

2


Input

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


Output

62

### Response

```cpp
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#define pb push_back
#define mp make_pair

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod pi=acos(-1);
const int oo=1<<30;
const LL OO=1e18;

const int N=1e6;

int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}

#define lc (i<<1)
#define rc (lc|1)
int tag[N];PR mi[N];
int a[N],b[N];
inline void build(int i,int l,int r) {
	mi[i]=mp(b[l],l);
	if (l!=r) {
		int mid=(l+r)>>1;
		build(lc,l,mid);
		build(rc,mid+1,r);
	}
}
inline void pushdown(int i) {
	if (tag[i]) {
		mi[lc].first+=tag[i],mi[rc].first+=tag[i];
		tag[lc]+=tag[i],tag[rc]+=tag[i];
		tag[i]=0;
	}
}
inline void modify(int i,int l,int r,int L,int R) {
	if (L<=l&&r<=R) tag[i]++,mi[i].first++;
	else {
		int mid=(l+r)>>1;
		pushdown(i);
		if (L<=mid) modify(lc,l,mid,L,R);
		if (mid<R) modify(rc,mid+1,r,L,R);
		mi[i]=min(mi[lc],mi[rc]);
	}
}
inline PR query(int i,int l,int r,int L,int R) {
	if (L<=l&&r<=R) return mi[i];
	int mid=(l+r)>>1;PR ans=mp(oo,0);
	pushdown(i);
	if (L<=mid) upmin(ans,query(lc,l,mid,L,R));
	if (mid<R) upmin(ans,query(rc,mid+1,r,L,R));
	return ans;
}
int main()
{
	int n=gi(),i,len,k;PR p;LL ans=0;
	for (i=1;i<=n;i++) a[i]=b[i]=gi();
	sort(b+1,b+1+n);
	len=unique(b+1,b+1+n)-b-1;
	build(1,1,len);
	for (i=1;i<=n;i++) {
		k=lower_bound(b+1,b+len+1,a[i])-b;
		p=query(1,1,len,k,len);
		ans+=p.first-a[i];
		if (p.second!=1)
			modify(1,1,len,1,p.second-1);
	}
	cout<<ans<<endl;
	return 0;
}

```