#include <bits/stdc++.h>
using namespace std;
long long w[100100];
long long a[100100];
int main() {
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long mmax = 0;
  for (int i = n - 1; i >= 0; i--) {
    mmax--;
    if (mmax > 0) {
      w[i] = mmax;
    }
    mmax = max(mmax, a[i] + 1);
  }
  long long tot = 0;
  long long sol = 0;
  for (int i = 0; i < n; i++) {
    if (tot < w[i]) {
      sol += tot - a[i];
      tot++;
    } else {
      sol += max((long long)0, tot - a[i] - 1);
      if (tot == a[i]) tot++;
    }
  }
  cout << sol << endl;
  return 0;
}
