#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  long long n, maxy = 0, count = 0, ans = 0;
  vector<long long> v, d;
  cin >> n;
  d.resize(n);
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (v[i] + 1 > maxy) maxy = v[i] + 1;
    d[i] = maxy;
    maxy--;
  }
  for (int i = 0; i < n; i++) {
    if (count < d[i]) {
      count++;
    }
    ans += count - v[i] - 1;
  }
  cout << ans;
  return 0;
}
