#include <bits/stdc++.h>
using namespace std;
long long a[100005], b[100005];
int main() {
  int n;
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
    b[0] = 1;
    for (int i = 1; i < n; i++) {
      if (b[i - 1] > a[i] + 1)
        b[i] = b[i - 1];
      else
        b[i] = a[i] + 1;
    }
    for (int i = n - 2; i >= 0; i--) {
      if (b[i + 1] - b[i] > 1) b[i] = b[i + 1] - 1;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++) sum += b[i] - a[i] - 1;
    printf("%I64d\n", sum);
  }
}
