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