#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int n) {
  long long int p = 1;
  while (n > 0) {
    if (n & 1) {
      p = ((p % 1000000007) * (a % 1000000007)) % 1000000007;
    }
    n >>= 1;
    a = ((a % 1000000007) * (a % 1000000007)) % 1000000007;
  }
  return p % 1000000007;
}
long long int inf = 10000000;
const long long B = 100000007;
const long long C = 10010107;
const int N = 500005;
long long int a[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  long long int mrk = 0, ans = 0;
  mrk = 1;
  stack<long long int> s;
  for (int i = 2; i <= n; i++) {
    if (mrk == a[i]) {
      mrk++;
      continue;
    }
    if (mrk > a[i]) {
      s.push(i);
      ans += (mrk - a[i] - 1);
      continue;
    }
    while (!s.empty() && mrk < a[i]) {
      long long int tp = s.top();
      mrk++;
      s.pop();
      ans += (i - tp);
    }
    mrk++;
  }
  printf("%lld\n", ans);
}
