#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> m(n);
  for (long long& mm : m) cin >> mm;
  vector<long long> pref_max(n);
  pref_max[0] = m[0] + 1;
  for (long long i = 1; i < n; ++i)
    pref_max[i] = max(pref_max[i - 1], m[i] + 1);
  long long ans = 0;
  long long cur = pref_max.back();
  for (long long i = n - 1; i >= 0; --i) {
    cur = max(cur - 1, pref_max[i]);
    ans += (cur - (m[i] + 1));
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
