#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
long long int a[N], pre[N], fm[N], cz[N];
int main() {
  long long int n;
  cin >> n;
  long long int cur = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int mmax = 0;
  for (int i = n - 1; i >= 0; i--) {
    mmax = max(mmax, a[i] + 1);
    pre[i] = mmax;
    mmax--;
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    if (cur < pre[i]) cur++;
    ans += cur - a[i] - 1;
  }
  cout << ans << endl;
  return 0;
}
