#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N], d[N];
int main() {
  int n;
  scanf("%d", &n);
  stack<int> s;
  int cur = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    s.push(i);
    if (a[i] + 1 > cur) {
      int need = a[i] + 1 - cur;
      while (need--) {
        d[s.top()] = 1;
        s.pop();
        cur++;
      }
    }
  }
  long long ans = 0;
  int marks = 0;
  for (int i = 1; i <= n; i++) {
    marks += d[i];
    ans += marks - a[i] - 1;
  }
  printf("%lld\n", ans);
  return 0;
}
