#include <bits/stdc++.h>
using namespace std;
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = numeric_limits<double>::infinity();
const int MOD = 1000000007;
const double EPS = 1e-9;
const double PI = acos(-1.0);
const int DX[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int DY[] = {0, 1, 0, -1, 1, -1, -1, 1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long sqr(long long x) { return x * x; }
long long sqr(int x) { return (long long)x * x; }
double sqr(double x) { return x * x; }
long double sqr(long double x) { return x * x; }
mt19937 mmtw(960172);
long long rnd(long long x, long long y) {
  static uniform_int_distribution<long long> d;
  return d(mmtw) % (y - x + 1) + x;
}
const int N = 100179;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  long long ans = 0;
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
  vector<int> need(n);
  for (int i = 0; i < n; ++i) need[i] = a[i] + 1;
  for (int i = n - 1; i > 0; --i) need[i - 1] = max(need[i - 1], need[i] - 1);
  int cc = 0;
  vector<int> add(n, 0);
  for (int i = 0; i < n; ++i) {
    if (need[i] > cc) {
      ++cc;
      add[i] = 1;
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (add[i]) {
      ans += cnt - a[i];
      ++cnt;
    } else {
      ans += cnt - a[i] - 1;
    }
  }
  cout << ans << "\n";
  return 0;
}
