#include <bits/stdc++.h>
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() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &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);
  }
  printf("%lld\n", ans);
  return 0;
}
