#include <bits/stdc++.h>
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long m[n];
  for (long long i = 0; i < n; ++i) {
    cin >> m[i];
  }
  long long t[n];
  for (long long i = 0; i < n; ++i) {
    if (i > 0)
      t[i] = max(t[i - 1], m[i] + 1);
    else
      t[i] = m[i] + 1;
  }
  long long counter = 0;
  for (long long i = n - 1; i >= 0; --i) {
    counter = max(counter - 1, t[i]);
    t[i] = counter;
  }
  long long sum = 0;
  for (long long i = 0; i < n; ++i) {
    sum += t[i] - m[i] - 1;
  }
  cout << sum << "\n";
}
int main() {
  bool testing = false;
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (testing) {
    freopen("test.txt", "rt", stdin);
    int start = clock();
    solve();
    int end = clock();
    cout << "time: " << (end - start) / (double)(CLOCKS_PER_SEC)*1000
         << " milliseconds\n";
  } else {
    solve();
  }
}
