#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> m(n);
  for (int i = 0; i < n; i++) {
    cin >> m[i];
  }
  set<int> free;
  int have = 0;
  for (int i = 0; i < n; i++) {
    while (have < m[i]) {
      have++;
      free.erase(*free.rbegin());
    }
    free.insert(i);
  }
  vector<bool> c(n, true);
  for (int i : free) {
    c[i] = false;
  }
  long long ans = 0;
  have = 0;
  for (int i = 0; i < n; i++) {
    ans += have - m[i];
    if (c[i]) {
      have++;
    }
  }
  cout << ans << endl;
  return 0;
}
