#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9 + 7;
const int maxn = 1e5 + 5;
int n, m;
int a[maxn];
int num[maxn];
int main() {
  cin >> n;
  long long sum1 = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum1 += a[i];
  }
  num[n] = a[n];
  long long sum2 = 0;
  for (int i = n - 1; i >= 1; i--) {
    num[i] = max(a[i], num[i + 1] - 1);
  }
  for (int i = 1; i < n; i++) {
    if (num[i] > num[i + 1]) {
      num[i + 1] = num[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    sum2 += num[i] + 1;
  }
  cout << sum2 - sum1 - n << endl;
  return 0;
}
