#include <bits/stdc++.h>
const int mod = 1e9 + 9;
const long long inf = 0x3f3f3f3f3f3f3f;
const int maxn = 1e5 + 5;
using namespace std;
int n, m;
int a[maxn], b[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  b[n] = a[n] + 1;
  for (int i = n - 1; i >= 1; i--) {
    b[i] = max(b[i + 1] - 1, a[i] + 1);
  }
  int tmp = 0;
  for (int i = 1; i <= n; i++) {
    tmp = max(tmp, b[i]);
    b[i] = tmp;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (b[i] - a[i] - 1);
  }
  printf("%lld\n", ans);
  return 0;
}
