#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 228;
const long long md = 1e9 + 7;
bool us[100][100];
int main(int argc, char** argv) {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n;
  vector<int> v(n);
  vector<int> d(n);
  vector<int> zeros;
  int cnt = 0;
  int h = 0;
  for (int i = 0; i < n; i++) {
    int& e = v[i];
    cin >> e;
    zeros.push_back(i);
    while (h <= e) {
      d[zeros.back()]++;
      h++;
      zeros.pop_back();
    }
  }
  h = 0;
  long long answer = 0;
  for (int i = 0; i < n; i++) {
    long long old = answer;
    if (d[i]) {
      h++;
      if (v[i] == 0)
        answer += h - 1;
      else if (v[i] == h - 1)
        answer += 0;
      else
        answer += h - v[i] - 1;
    } else {
      answer += h - v[i] - 1;
    }
  }
  cout << answer << "\n";
  return 0;
}
