#include <bits/stdc++.h>
using namespace std;
const int md = 1e5 + 10;
long long res;
int a[md], d[md];
int n, now;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    now = max(now, a[i] + 1);
    d[i] = now;
  }
  for (int i = n - 1; i >= 1; i--) {
    d[i] = max(d[i], d[i + 1] - 1);
  }
  for (int i = 1; i <= n; i++) res += max(d[i] - a[i], 0);
  cout << res - n;
  return 0;
}
