#include <bits/stdc++.h>
int n;
long long int sum;
int m[100010], limit[100010];
int max(int a, int b) { return (a > b ? a : b); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &m[i]);
  for (int i = n; i > 0; i--) limit[i] = max(limit[i + 1] - 1, m[i] + 1);
  int current = 1;
  for (int i = 1; i <= n; i++) {
    current = max(current, limit[i]);
    sum += (current - m[i] - 1);
  }
  printf("%lld", sum);
}
