#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, ans = 0, i;
  cin >> n;
  long long a[n], t[n], cur = 0;
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = n - 1; i >= 0; i--) {
    cur = max((long long)0, cur - 1);
    cur = max(cur, a[i] + 1);
    t[i] = cur;
  }
  cur = 0;
  for (i = 0; i < n; i++) {
    cur = max(cur, t[i]);
    ans += cur - a[i] - 1;
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
