#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
using llong = long long;
using ld = long double;
using itn = int;
using ii = pair<int, int>;
using ull = unsigned long long;
using pll = pair<llong, llong>;
using psi = pair<string, int>;
using ll = pair<llong, llong>;
const llong over999 = 1e9;
const ld over999ld = 1e18 + 1;
const llong md = 1e9 + 7;
const ld EPS = 1e-15;
const ld Pi = acos(-1);
const ld nich = 0;
const llong mnogo = 300;
long long n, x[100100], pr[100010];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  long long now = 1;
  for (long long i = 1; i <= n; i++) {
    cin >> x[i];
    if (x[i] >= now) {
      now = x[i] + 1;
    }
    pr[i] = max(pr[i - 1], now);
  }
  long long all = 0;
  for (long long i = n; i >= 1; i--) {
    if (pr[i] < now) {
      now--;
    }
    all += now;
  }
  for (long long i = 1; i <= n; i++) {
    all -= x[i] + 1;
  }
  cout << all;
  return 0;
}
