#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
bool umin(T1& x, const T2& y) {
  return (x > y ? (x = y, true) : false);
}
template <typename T1, typename T2>
bool umax(T1& x, const T2& y) {
  return (x < y ? (x = y, true) : false);
}
template <typename T>
void read(T& x) {
  char ch;
  T p = 1;
  x = 0;
  do {
    ch = getchar();
  } while (ch <= ' ');
  if (ch == '-') p = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= p;
}
void read(string& x) {
  char ch;
  do {
    ch = getchar();
  } while (ch <= ' ');
  while (ch > ' ') x += ch, ch = getchar();
}
void read(char& x) {
  do {
    x = getchar();
  } while (x <= ' ');
}
template <typename T, typename... R>
void read(T& x, R&... y) {
  read(x);
  read(y...);
}
const int N = (int)1e6 + 3;
const int INF = (int)1e9 + 17;
const long long mod = (long long)1e9 + 7;
const long long LLINF = (long long)1e18 + 17;
const long double pi = (long double)acos(-1.0);
int n, m;
int a[N];
bool add[N];
int main() {
  read(n);
  for (int i = (0); i < (n); i++) read(a[i]);
  long long ans = 0;
  int sz = *max_element(a, a + n);
  int idx = -1;
  for (int i = (0); i < (n); i++) {
    if (a[i] == sz) {
      idx = i;
      break;
    }
  }
  if (sz == 0) {
    printf("%d\n", 0);
    return 0;
  }
  int mn = -1;
  int addsz = 0;
  for (int i = (0); i < (n); i++) {
    if (umax(mn, a[i])) {
      add[i] = true;
    }
  }
  set<int> zero;
  for (int i = (0); i < (n); i++) {
    while (a[i] > addsz) {
      add[*prev(zero.end())] = true;
      zero.erase(prev(zero.end()));
      ++addsz;
    }
    addsz += add[i];
    if (!add[i]) zero.insert(i);
  }
  int all = 0;
  for (int i = (0); i < (n); i++) {
    all += add[i];
    ans += all - a[i] - 1;
  }
  printf("%I64d\n", ans);
  return 0;
}
