#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const long long int MAX = -1000000000000000000;
const long long int MIN = 1000000000000000000;
const long long int inf = 1000000000;
const long long int KOK = 100000;
const long long int LOG = 30;
const long long int li = 100005;
const long long int mod = 1000000007;
long long int n, m, cev, b[li], a[li], k, mx = -inf, sira;
string s;
map<long long int, long long int> mpp;
vector<long long int> v;
inline long long int solve() {
  scanf("%lld", &n);
  sira = n;
  for (long long int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    mx = max(mx, a[i]);
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  for (long long int i = n; i >= 1; i--) {
    while (mpp[b[sira]] > 0) {
      mpp[b[sira]]--;
      sira--;
    }
    mx = max(mx - 1, b[sira]);
    cev += mx - a[i];
    mpp[a[i]]++;
    while (mpp[b[sira]] > 0) {
      mpp[b[sira]]--;
      sira--;
    }
    mx = max(mx, b[sira]);
  }
  printf("%lld\n", cev);
  return 0;
}
int main(void) {
  solve();
  return 0;
}
