#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;


int a[200010], b[200010];
int n, bs;


int mn[800010], tag[800010];
void build(int p, int l, int r) {
	if(l == r) {
		mn[p] = b[l]; tag[p] = 0;
		return ;
	}
	int mid = (l + r)>>1;
	build(p<<1, l, mid); build(p<<1|1, mid + 1, r);
	mn[p] = min(mn[p<<1], mn[p<<1|1]);
}
inline void pd(int p) {
	tag[p<<1]+= tag[p]; mn[p<<1]+= tag[p];
	tag[p<<1|1]+= tag[p]; mn[p<<1|1]+= tag[p];
	tag[p] = 0;
}
void add(int p, int l, int r, int x, int y) {
	if(x > y) return ;
	if(x <= l && r <= y) {
		++tag[p];
		++mn[p];
		return ;
	}
	int mid = (l + r)>>1;
	pd(p); 
	if(x <= mid) add(p<<1, l, mid, x, y);
	if(mid + 1 <= y) add(p<<1|1, mid + 1, r, x, y);
	mn[p] = min(mn[p<<1], mn[p<<1|1]);
}
int query(int p, int l, int r, int x, int y) {
	if(x <= l && r <= y) return mn[p];
	int mid = (l + r)>>1;
	int s1 = 0x7fffffff, s2 = 0x7fffffff;
	pd(p);
	if(x <= mid) s1 = query(p<<1, l, mid, x, y);
	if(mid + 1 <= y) s2 = query(p<<1|1, mid + 1, r, x, y);
	return min(s1, s2);
}
int main() {
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> a[i];
	memcpy(b, a, sizeof(b)); sort(b+1, b+n+1); bs = unique(b+1, b+n+1) - b - 1;
	build(1, 1, bs);
	long long ans = 0;
	for(int i = 1; i <= n; ++i) {
		int p = lower_bound(b+1, b+bs+1, a[i]) - b;
		ans+= query(1, 1, bs, p, bs) - a[i];
		add(1, 1, bs, 1, p - 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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;


int a[200010], b[200010];
int n, bs;


int mn[800010], tag[800010];
void build(int p, int l, int r) {
	if(l == r) {
		mn[p] = b[l]; tag[p] = 0;
		return ;
	}
	int mid = (l + r)>>1;
	build(p<<1, l, mid); build(p<<1|1, mid + 1, r);
	mn[p] = min(mn[p<<1], mn[p<<1|1]);
}
inline void pd(int p) {
	tag[p<<1]+= tag[p]; mn[p<<1]+= tag[p];
	tag[p<<1|1]+= tag[p]; mn[p<<1|1]+= tag[p];
	tag[p] = 0;
}
void add(int p, int l, int r, int x, int y) {
	if(x > y) return ;
	if(x <= l && r <= y) {
		++tag[p];
		++mn[p];
		return ;
	}
	int mid = (l + r)>>1;
	pd(p); 
	if(x <= mid) add(p<<1, l, mid, x, y);
	if(mid + 1 <= y) add(p<<1|1, mid + 1, r, x, y);
	mn[p] = min(mn[p<<1], mn[p<<1|1]);
}
int query(int p, int l, int r, int x, int y) {
	if(x <= l && r <= y) return mn[p];
	int mid = (l + r)>>1;
	int s1 = 0x7fffffff, s2 = 0x7fffffff;
	pd(p);
	if(x <= mid) s1 = query(p<<1, l, mid, x, y);
	if(mid + 1 <= y) s2 = query(p<<1|1, mid + 1, r, x, y);
	return min(s1, s2);
}
int main() {
	cin >> n;
	for(int i = 1; i <= n; ++i) cin >> a[i];
	memcpy(b, a, sizeof(b)); sort(b+1, b+n+1); bs = unique(b+1, b+n+1) - b - 1;
	build(1, 1, bs);
	long long ans = 0;
	for(int i = 1; i <= n; ++i) {
		int p = lower_bound(b+1, b+bs+1, a[i]) - b;
		ans+= query(1, 1, bs, p, bs) - a[i];
		add(1, 1, bs, 1, p - 1);
	}
	cout << ans << endl;
	return 0;
}
```