#include <bits/stdc++.h>
using namespace std;
long long n, a[100000], b[100000];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  b[0] = a[0];
  for (int i = 1; i < n; i++) {
    b[i] = max(b[i - 1], a[i]);
  }
  long long res = 0, mth = 0;
  for (int i = n - 1; i >= 0; i--) {
    mth = max(mth - 1, b[i]);
    res += mth - a[i];
  }
  cout << res << endl;
  return 0;
}
