#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 100000000000000;
bool intersect(string& a, string& b) {
  for (int i = 0; i < a.size(); ++i) {
    if (a[i] == '#' && b[i] == '#') return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  vector<ll> min_k(n), upper(n);
  for (ll& e : upper) cin >> e;
  min_k[0] = 1;
  for (int i = 1; i < n; ++i) {
    min_k[i] = max(min_k[i - 1], upper[i] + 1);
  }
  ll answ = 0;
  for (int i = n - 1; i > 0; --i) {
    answ += min_k[i] - upper[i] - 1;
    min_k[i - 1] = max(min_k[i - 1], min_k[i] - 1);
  }
  cout << answ << "\n";
  return 0;
}
