#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000;
const int MAXN = 100000;
bool shouldAdd[MAXN];
int main() {
  int n;
  cin >> n;
  vector<int> d(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
  }
  stack<int> virt;
  long long total = 0;
  for (int i = 0; i < n; i++) {
    if (total - 1 >= d[i]) {
      virt.push(i);
    } else {
      while (total < d[i]) {
        shouldAdd[virt.top()] = true;
        virt.pop();
        total++;
      }
      shouldAdd[i] = true;
      total++;
    }
  }
  total = 0;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (shouldAdd[i]) {
      total++;
    }
    ans += total - 1 - d[i];
  }
  cout << ans;
  return 0;
}
