#include <bits/stdc++.h>
using namespace std;
int main() {
  long long ans = 0;
  int n, i, j;
  cin >> n;
  int m[n], now, w[n];
  for (i = 0; i < n; i++) cin >> m[i];
  now = m[n - 1] + 1;
  for (i = n - 1; i >= 0; i--) {
    now = (((m[i] + 1) > (now)) ? (m[i] + 1) : (now));
    w[i] = now;
    now--;
  }
  now = w[0];
  for (i = 0; i < n; i++) {
    now = (((now) > (w[i])) ? (now) : (w[i]));
    ans += now - m[i] - 1;
  }
  cout << ans;
}
