#include <bits/stdc++.h>
using namespace std;
struct cosa {
  int words;
  int next[26];
};
struct cost {
  long long int a, b, c;
};
struct q {
  int a;
  string b;
};
int alfa, beta;
char buffer[10];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool operator<(const cost &a, const cost &b) {
  if (a.a == b.a) {
    if (a.b == b.b) {
      return a.c < b.c;
    }
    return a.b < b.b;
  }
  return a.a < b.a;
}
string k;
long long int troy[1000000];
long long int act[1000000];
long long int sum[1000000];
int main() {
  char prof;
  ios_base::sync_with_stdio(0);
  long long int x = -1, y = 0, z, mal = 2000000000, s = 0, d = -2, a1 = 0, b1,
                c1, x1, y1, t, res = 0;
  long long int a, b, c, e, f;
  cin >> a;
  for (int i = int(0); i < int(a); i++) {
    cin >> troy[i];
    if (troy[i] > x) {
      x = troy[i];
      act[i] = 1;
    }
  }
  if (a == 1) {
    cout << "0";
    return 0;
  }
  b = a;
  x = 0;
  b--;
  for (int i = int(0); i < int(a); i++) {
    if (troy[i] > x) {
      y = i - 1;
      while (troy[i] > x) {
        if (sum[y] != 0) {
          y = sum[y];
        }
        if (act[y] == 0) {
          x++;
          act[y] = 1;
        }
        y--;
      }
      sum[i] = y;
    }
    x += act[i];
  }
  x = 0;
  res = 0;
  for (int i = int(0); i < int(a); i++) {
    x += act[i];
    res += x - troy[i] - 1;
  }
  cout << res;
}
