#include <bits/stdc++.h>
using namespace std;
long long arr[1000000];
int main() {
  long long n;
  cin >> n;
  long long maxi = 0;
  multiset<long long, greater<long long> > mi;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
    maxi = max(maxi, arr[i]);
    mi.insert(arr[i]);
  }
  long long tot = 0;
  for (long long i = n - 1; i >= 0; i--) {
    if (maxi > *mi.begin()) maxi--;
    tot += maxi - arr[i];
    mi.erase(mi.find(arr[i]));
  }
  cout << tot;
  return 0;
}
