#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    long long n;
    cin >> n;
    vector<long long int> v;
    vector<long long int> a(n), b, c;
    for (long long int i = 0; i < n; i++) {
      long long x;
      cin >> x;
      v.push_back(x);
    }
    long long t = -1;
    for (long long int i = 0; i < n; i++) {
      if (v[i] > t) {
        a[i] = v[i] + 1;
        b.push_back(i);
        t = v[i];
      }
    }
    a.push_back(t + 1 + 1);
    b.push_back(n);
    for (int i = b.size() - 1; i > 0; i--) {
      long long r = a[b[i]], l = a[b[i - 1]];
      for (long long j = b[i] - 1; j >= b[i - 1]; j--) {
        r--;
        a[j] = max(l, r);
      }
    }
    long long ans = 0;
    for (long long int i = 0; i < n; i++) {
      ans += a[i] - 1 - v[i];
    }
    cout << ans;
  }
  return 0;
}
