#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  set<long long> q;
  vector<long long> v1(n, 0);
  long long len = 0;
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (long long i = 0; i < n; i++) {
    while (len < v[i]) {
      len++;
      v1[-(*(q.begin()))] = 1;
      q.erase(q.begin());
    }
    if (len == v[i]) {
      v1[i] = 1;
      len++;
    } else
      q.insert(-i);
  }
  len = 0;
  long long res = 0;
  for (long long i = 0; i < n; i++) {
    if (v1[i] == 1) {
      res += len - v[i];
      len++;
    } else {
      res += len - v[i] - 1;
    }
  }
  cout << res << endl;
  return 0;
}
