#include <bits/stdc++.h>
using namespace std;
void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void one();
int main() {
  fastio();
  int t = 1;
  for (int i = 0; i < t; ++i) {
    one();
  }
  return 0;
}
void one() {
  int n;
  cin >> n;
  vector<long long> m(n);
  for (int i = 0; i < n; ++i) cin >> m[i];
  vector<long long> t(n);
  for (int i = 0; i < n; ++i) {
    t[i] = m[i] + 1;
    if (i > 0) {
      t[i] = max(t[i], t[i - 1]);
    }
  }
  long long mx = t[n - 1];
  for (int i = n - 1; i >= 0; --i) {
    if (t[i] >= mx - 1) {
      mx = t[i];
    } else {
      mx--;
      t[i] = mx;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int d = t[i] - 1 - m[i];
    ans += d;
  }
  cout << ans << "\n";
}
