#include <bits/stdc++.h>
using namespace std;
long long int arr[100005];
bool pos[100005];
vector<int> vec;
int main() {
  long long int n, j, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long int cur = 0;
  long long int buf = 0;
  for (i = 0; i < n; i++) {
    if (arr[i] == cur) {
      cur++;
    } else if (arr[i] < cur) {
      pos[i] = 1;
      vec.push_back(i);
    } else if (arr[i] > cur) {
      pos[i] = 1;
      vec.push_back(i);
      while (vec.size()) {
        if (cur == arr[i] + 1) break;
        j = vec.size();
        j = vec[j - 1];
        cur++;
        pos[j] = 0;
        vec.pop_back();
      }
    }
  }
  cur = 0;
  long long int tot = 0;
  for (i = 0; i < n; i++) {
    if (pos[i] == 0) cur++;
    tot = tot + cur - (arr[i] + 1);
  }
  cout << tot;
}
