#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<55;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

class Lazy_Segment_Tree{
	private:
	int n;
	vp date;
	vi lazy;
	void Set_Lazy(int k,int x){
		date[k].first+=x;
		lazy[k]+=x;
	}
	void Push(int k){
		Set_Lazy(k*2+1,lazy[k]);
		Set_Lazy(k*2+2,lazy[k]);
		lazy[k]=0;
	}
	void Fix(int k){
		date[k]=min(date[k*2+1],date[k*2+2]);
	}
	void Add_func(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return;
		if(a<=l&&r<=b){
			Set_Lazy(k,-1);
			return;
		}
		Push(k);
		int m=(l+r)/2;
		Add_func(a,b,k*2+1,l,m);
		Add_func(a,b,k*2+2,m,r);
		Fix(k);
	}
	P Query_func(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return {inf,0};
		if(a<=l&&r<=b) return date[k];
		Push(k);
		int m=(l+r)/2;
		P vl=Query_func(a,b,k*2+1,l,m);
		P vr=Query_func(a,b,k*2+2,m,r);
		return min(vl,vr);
	}
	public:
	Lazy_Segment_Tree(int n_,vi a){
		n=1;
		while(n<n_) n*=2;
		date=vp(2*n-1,{inf,inf});
		lazy=vi(2*n-1);
		for(int i=0;i<n_;i++) date[i+n-1]={a[i],i};
		for(int i=n-2;i>=0;i--) Fix(i);
	}
	void Add(int a,int b){
		Add_func(a,b,0,0,n);
	}
	P Query(int a,int b){
		return Query_func(a,b,0,0,n);
	}
};

class Segment_Tree{
	private:
	int n;
	vi date;
	int Rec(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return 0;
		if(a<=l&&r<=b) return date[k];
		int m=(l+r)/2;
		return Rec(a,b,k*2+1,l,m)+Rec(a,b,k*2+2,m,r);
	}
	public:
	Segment_Tree(int n_){
		n=1;
		while(n<n_) n*=2;
		date=vi(2*n-1);
	}
	void Update(int k){
		k+=n-1;
		date[k]++;
		while(k>0){
			k=(k-1)/2;
			date[k]=date[k*2+1]+date[k*2+2];
		}
	}
	int Query(int a,int b){
		return Rec(a,b,0,0,n);
	}
	int Open(int k){
		return date[k+n-1];
	}
};

int n,N;
vi a,b;
map<int,int> m;

int main(){
	cin>>n;
	a=vi(n);
	for(int i=0;i<n;i++)cin>>a[i];
	b=a;
	sort(b.begin(),b.end());
	b.erase(unique(b.begin(),b.end()),b.end());
	N=b.size();
	Lazy_Segment_Tree lst(N,b);
	Segment_Tree st(N);
	ll res=0;
	for(int i=0;i<N;i++) m[b[i]]=i;
	for(int i=0;i<n;i++){
		P p=lst.Query(m[a[i]],N);
		int I=p.second;
		res+=b[I]-a[i];
		a[i]=b[I];
		lst.Add(I,N);
		res+=st.Query(I+1,N);
		st.Update(I);
	}
	cout<<res<<endl;
}
### Prompt

Generate a Cpp solution to the following problem:

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 <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<55;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

class Lazy_Segment_Tree{
	private:
	int n;
	vp date;
	vi lazy;
	void Set_Lazy(int k,int x){
		date[k].first+=x;
		lazy[k]+=x;
	}
	void Push(int k){
		Set_Lazy(k*2+1,lazy[k]);
		Set_Lazy(k*2+2,lazy[k]);
		lazy[k]=0;
	}
	void Fix(int k){
		date[k]=min(date[k*2+1],date[k*2+2]);
	}
	void Add_func(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return;
		if(a<=l&&r<=b){
			Set_Lazy(k,-1);
			return;
		}
		Push(k);
		int m=(l+r)/2;
		Add_func(a,b,k*2+1,l,m);
		Add_func(a,b,k*2+2,m,r);
		Fix(k);
	}
	P Query_func(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return {inf,0};
		if(a<=l&&r<=b) return date[k];
		Push(k);
		int m=(l+r)/2;
		P vl=Query_func(a,b,k*2+1,l,m);
		P vr=Query_func(a,b,k*2+2,m,r);
		return min(vl,vr);
	}
	public:
	Lazy_Segment_Tree(int n_,vi a){
		n=1;
		while(n<n_) n*=2;
		date=vp(2*n-1,{inf,inf});
		lazy=vi(2*n-1);
		for(int i=0;i<n_;i++) date[i+n-1]={a[i],i};
		for(int i=n-2;i>=0;i--) Fix(i);
	}
	void Add(int a,int b){
		Add_func(a,b,0,0,n);
	}
	P Query(int a,int b){
		return Query_func(a,b,0,0,n);
	}
};

class Segment_Tree{
	private:
	int n;
	vi date;
	int Rec(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return 0;
		if(a<=l&&r<=b) return date[k];
		int m=(l+r)/2;
		return Rec(a,b,k*2+1,l,m)+Rec(a,b,k*2+2,m,r);
	}
	public:
	Segment_Tree(int n_){
		n=1;
		while(n<n_) n*=2;
		date=vi(2*n-1);
	}
	void Update(int k){
		k+=n-1;
		date[k]++;
		while(k>0){
			k=(k-1)/2;
			date[k]=date[k*2+1]+date[k*2+2];
		}
	}
	int Query(int a,int b){
		return Rec(a,b,0,0,n);
	}
	int Open(int k){
		return date[k+n-1];
	}
};

int n,N;
vi a,b;
map<int,int> m;

int main(){
	cin>>n;
	a=vi(n);
	for(int i=0;i<n;i++)cin>>a[i];
	b=a;
	sort(b.begin(),b.end());
	b.erase(unique(b.begin(),b.end()),b.end());
	N=b.size();
	Lazy_Segment_Tree lst(N,b);
	Segment_Tree st(N);
	ll res=0;
	for(int i=0;i<N;i++) m[b[i]]=i;
	for(int i=0;i<n;i++){
		P p=lst.Query(m[a[i]],N);
		int I=p.second;
		res+=b[I]-a[i];
		a[i]=b[I];
		lst.Add(I,N);
		res+=st.Query(I+1,N);
		st.Update(I);
	}
	cout<<res<<endl;
}
```