#include <bits/stdc++.h>
using namespace std;
void print() { cerr << "]\n"; }
template <typename T, typename... ARGS>
void print(T &x, ARGS... args) {
  cerr << to_string(x);
  if (sizeof...(args)) cerr << ", ";
  print(args...);
}
const long long inf = 1e9 + 7;
const long long mxN = 1e5 + 1;
void solve() {
  long long n;
  cin >> n;
  vector<long long> level(n), req(n), ach(n);
  for (long long i = 0; i < n; i++) {
    cin >> level[i];
    req[i] = level[i] + 1;
  }
  for (long long i = n - 2; i >= 0; i--) {
    req[i] = max(req[i], req[i + 1] - 1);
  }
  long long cnt = 0;
  for (long long i = 0; i < n; i++) {
    ach[i] = cnt = max(cnt, req[i]);
  }
  long long res = 0;
  for (long long i = 0; i < n; i++) {
    res += ach[i] - level[i] - 1;
  }
  cout << res << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long T = 1, t = 0;
  while (t++ < T) {
    solve();
  }
}
