#include <bits/stdc++.h>
using namespace std;
int n;
long long int a[100052], h[100052], b[100052], sum;
int main() {
  scanf("%d", &n);
  sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &h[i]);
    b[i] = max(b[i - 1], h[i] + 1);
    sum += h[i];
  }
  for (int i = n - 1; i > 0; i--) {
    b[i] = max(b[i], b[i + 1] - 1);
  }
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += b[i];
  }
  printf("%lld\n", ans - sum - n);
}
