#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  long long maxim[n];
  maxim[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    maxim[i] = max(ar[i + 1], maxim[i + 1] - 1);
  }
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    maxim[i] = max(maxim[i], maxim[i - 1]);
  }
  maxim[0] = 1;
  for (int i = 1; i < n; i++) {
    if (ar[i] == maxim[i - 1]) {
      maxim[i] = maxim[i - 1] + 1;
    } else {
      maxim[i] = max(maxim[i], maxim[i - 1]);
    }
  }
  for (int i = 0; i < n; i++) {
    ans += maxim[i] - ar[i] - 1;
  }
  cout << ans << endl;
}
