#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, m;
int f[N], a[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]++;
  }
  int l = 1;
  long long kq = 0;
  vector<int> o;
  for (int i = 2; i <= n; i++) {
    if (a[i] > l) {
      while (o.size() && (a[i] - 1) > l) {
        int w = o[o.size() - 1];
        o.pop_back();
        kq += i - w;
        l++;
      }
      l++;
    } else {
      kq += l - a[i];
      o.push_back(i);
    }
  }
  cout << kq << endl;
  return 0;
}
