#include <bits/stdc++.h>
using namespace std;
const int N = 112345;
long long n;
long long ans, tot;
priority_queue<long long> pq;
int main(void) {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long x;
    cin >> x;
    pq.emplace(i);
    while (x >= tot) {
      ans += i - pq.top();
      pq.pop();
      tot++;
    }
    ans += tot - x - 1;
  }
  cout << ans << endl;
  return 0;
}
