#include <bits/stdc++.h>
using namespace std;
const int OO = 0x3f3f3f3f, N = 1e5 + 5, mod = 1e9 + 7;
const double pi = acos(-1), eps = 1e-9;
int n, x, c = 1;
vector<int> v;
long long ans;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &x);
    if (x >= c) {
      while (c < x) {
        ans += i - v.back();
        ++c;
        v.pop_back();
      }
      ++c;
    } else {
      v.push_back(i);
      ans += c - x - 1;
    }
  }
  printf("%lld", ans);
  return 0;
}
