#include <bits/stdc++.h>
using namespace std;
long long d[100100];
int main() {
  long long n;
  cin >> n;
  vector<long long> v;
  for (long long i = (0); i < (n); ++i) {
    long long a;
    cin >> a;
    v.push_back(a);
  }
  memset((d), (0), sizeof(d));
  for (long long i = (0); i < (v.size()); ++i) {
    d[i] = v[i];
  }
  for (long long i = (n - 1 - 1); i >= (0); --i) {
    d[i] = max(d[i], d[i + 1] - 1);
  }
  long long c = 0;
  long long ans = 0;
  for (long long i = (1); i < (v.size()); ++i) {
    long long a = v[i];
    long long b = d[i];
    if (c < b) {
      c++;
      ans += c - a;
    } else {
      if (c + 1 == a) {
        c++;
      } else {
        ans += c - a;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
