#include <bits/stdc++.h>
using namespace std;
int v[200005], d[200005];
int main() {
  int n, i, passei;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &v[i]);
  for (i = 0; i < n; i++) {
    d[i] = v[i] + 1;
    if (i) d[i] = max(d[i], d[i - 1]);
  }
  passei = 0;
  for (i = n - 2; i >= 0; i--) {
    passei = max(passei, v[i + 1]);
    d[i] = max(d[i], passei);
    passei--;
  }
  long long ans = 0LL;
  for (i = 0; i < n; i++) ans += (d[i] - v[i] - 1LL);
  printf("%lld\n", ans);
}
