#include <bits/stdc++.h>
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
using namespace std;
long long power(long long a, long long p, long long M) {
  long long r = 1;
  a = a % M;
  while (p > 0) {
    if (p % 2) r = (r * a) % M;
    a = (a * a) % M;
    p /= 2;
  }
  return r;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long arr[(int)1e5 + 5], n;
long long a[(int)1e5 + 5];
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) scanf("%lld", &arr[i]);
  ;
  a[0] = 1;
  for (long long i = 1; i < n; i++) {
    a[i] = max(a[i - 1], 1 + arr[i]);
  }
  for (int i = n - 2; i >= 0; i--) {
    a[i] = max(a[i], a[i + 1] - 1);
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    ans += max(0LL, a[i] - arr[i] - 1);
  }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
