#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e5 + 9;
const long long mod = (long long)1e9 + 7;
const double pi = acos(-1);
const double eps = 1e-15;
int n;
int m[maxn];
int t[maxn];
void init() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &m[i]);
  }
}
int main() {
  init();
  int cnt = 0;
  for (int i = n - 1; i >= 0; i--) {
    cnt = max(0, cnt - 1);
    cnt = max(cnt, m[i] + 1);
    t[i] = cnt;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cnt = max(cnt, t[i]);
    ans += cnt - m[i] - 1;
  }
  cout << ans << endl;
  return 0;
}
