#include <bits/stdc++.h>
using namespace std;
const int MAXN = 112345;
int m[MAXN];
int s[MAXN];
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &m[i]);
  }
  int cur_s = 0;
  for (int i = N; i > 0; i--) {
    cur_s = max(cur_s - 1, 0);
    cur_s = max(cur_s, m[i] + 1);
    s[i] = cur_s;
  }
  long long ans = 0;
  cur_s = 0;
  for (int i = 1; i <= N; i++) {
    cur_s = max(cur_s, s[i]);
    ans += (cur_s - m[i] - 1);
  }
  printf("%lld\n", ans);
  return 0;
}
