#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y) {
  long long res = 1LL;
  x = x % 1000000007;
  while (y > 0) {
    if (y & 1) res = (res * x) % 1000000007;
    y = y >> 1;
    x = (x * x) % 1000000007;
  }
  return res % 1000000007;
}
long long inv(long long n) { return power(n, 1000000007 - 2) % 1000000007; }
long long n, m[200005], t[200005];
void solve() {
  cin >> n;
  long long i;
  for (i = 0; i < n; i++) cin >> m[i];
  long long cur = 0;
  for (i = n - 1; i >= 0; i--) {
    cur = max({0LL, cur - 1, m[i] + 1});
    t[i] = cur;
  }
  for (i = 1; i < n; i++) t[i] = max(t[i], t[i - 1]);
  long long ans = 0;
  for (i = 0; i < n; i++) ans += t[i] - (m[i] + 1);
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
}
