#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAX = 1 << 19;
int n,nn;
PII dp[MAX];
int t[MAX],laz[MAX],fen[MAX],gd[MAX];
void add(int x){
  for(x++;x; x-=x&-x){
    fen[x]++;
  }
}
int sum(int x){
  int res = 0;
  for(x++;x < MAX; x+= x&-x){
    res += fen[x];
  }
  return res;
}

void touch(int nr){
  if(laz[nr]){
    laz[nr*2] += laz[nr];
    dp[nr*2].FI += laz[nr];
    laz[nr*2 + 1] += laz[nr];
    dp[nr*2 + 1].FI += laz[nr];
    laz[nr] = 0;
  }
}
int get_max(int a){
  debug("get_max", a);
  PII res = {(int)-1e15,0};
  function<void(int,int,int)> rec = [&](int nr,int beg,int end){
    if(end <= a)return;
    if(beg >= a){
      maxi(res, dp[nr]);
      return;
    }
    touch(nr);
    rec(nr * 2, beg, (beg + end) / 2);
    rec(nr * 2 + 1, (beg + end) / 2, end);
  };
  rec(1,0,nn);
  debug(res);
  return res.SE;
}
void dodaj(int a){
  function<void(int,int,int)> rec = [&](int nr,int beg,int end){
    if(end <= a)return;
    if(beg >= a){
      laz[nr] ++;
      dp[nr].FI ++;
      return;
    }
    touch(nr);
    rec(nr * 2, beg, (beg + end) / 2);
    rec(nr * 2 + 1, (beg + end) / 2, end);
    dp[nr] = max(dp[nr*2],dp[nr*2+1]);
  };
  rec(1,0,nn);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  set<int> secik;
  R(i,n){
    cin >> t[i];
    secik.insert(t[i]);
  }
  nn = 1;while(nn <= SZ(secik)) nn *= 2;
  int j = 0;
  map<int,int> scal;
  for(int el:secik){
    scal[el] = j;
    gd[j] = el;
    dp[j + nn] = {-el,j};
    j++;
  }
  for(int i = SZ(secik); i < nn; i++){
    dp[i + nn] = {(int)-1e12, 0ll};
  }
  for(int i = nn-1; i; i--){
    dp[i] = max(dp[i*2],dp[i*2+1]);
    debug(i, dp[i]);
  }
  int res = 0;
  R(i,n){
    int pom = get_max(scal[t[i]]);
    debug(pom,gd[pom] - t[i]);
    res += gd[pom] - t[i];
    debug(sum(pom + 1));
    res += sum(pom + 1);
    dodaj(pom);
    add(pom);
  }
  cout << res << "\n";
}

### Prompt

Create a solution in cpp for 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>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAX = 1 << 19;
int n,nn;
PII dp[MAX];
int t[MAX],laz[MAX],fen[MAX],gd[MAX];
void add(int x){
  for(x++;x; x-=x&-x){
    fen[x]++;
  }
}
int sum(int x){
  int res = 0;
  for(x++;x < MAX; x+= x&-x){
    res += fen[x];
  }
  return res;
}

void touch(int nr){
  if(laz[nr]){
    laz[nr*2] += laz[nr];
    dp[nr*2].FI += laz[nr];
    laz[nr*2 + 1] += laz[nr];
    dp[nr*2 + 1].FI += laz[nr];
    laz[nr] = 0;
  }
}
int get_max(int a){
  debug("get_max", a);
  PII res = {(int)-1e15,0};
  function<void(int,int,int)> rec = [&](int nr,int beg,int end){
    if(end <= a)return;
    if(beg >= a){
      maxi(res, dp[nr]);
      return;
    }
    touch(nr);
    rec(nr * 2, beg, (beg + end) / 2);
    rec(nr * 2 + 1, (beg + end) / 2, end);
  };
  rec(1,0,nn);
  debug(res);
  return res.SE;
}
void dodaj(int a){
  function<void(int,int,int)> rec = [&](int nr,int beg,int end){
    if(end <= a)return;
    if(beg >= a){
      laz[nr] ++;
      dp[nr].FI ++;
      return;
    }
    touch(nr);
    rec(nr * 2, beg, (beg + end) / 2);
    rec(nr * 2 + 1, (beg + end) / 2, end);
    dp[nr] = max(dp[nr*2],dp[nr*2+1]);
  };
  rec(1,0,nn);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  set<int> secik;
  R(i,n){
    cin >> t[i];
    secik.insert(t[i]);
  }
  nn = 1;while(nn <= SZ(secik)) nn *= 2;
  int j = 0;
  map<int,int> scal;
  for(int el:secik){
    scal[el] = j;
    gd[j] = el;
    dp[j + nn] = {-el,j};
    j++;
  }
  for(int i = SZ(secik); i < nn; i++){
    dp[i + nn] = {(int)-1e12, 0ll};
  }
  for(int i = nn-1; i; i--){
    dp[i] = max(dp[i*2],dp[i*2+1]);
    debug(i, dp[i]);
  }
  int res = 0;
  R(i,n){
    int pom = get_max(scal[t[i]]);
    debug(pom,gd[pom] - t[i]);
    res += gd[pom] - t[i];
    debug(sum(pom + 1));
    res += sum(pom + 1);
    dodaj(pom);
    add(pom);
  }
  cout << res << "\n";
}

```