#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct u {
  int x, y;
  u(int _x = 0, int _y = 0) : x(_x), y(_y) {}
  bool operator<(u a) const { return (x != a.x) ? (x < a.x) : (y > a.y); }
};
int f[N], m[N], n;
long long ans;
set<int> pos;
set<u> a;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &m[i]);
    a.insert(u(f[i] = m[i] + 1, i));
    pos.insert(i);
  }
  while (!pos.empty()) {
    u tmp = *prev(a.end());
    a.erase(tmp);
    for (set<int>::iterator it = pos.lower_bound(tmp.y); it != pos.end();
         it = pos.lower_bound(tmp.y)) {
      f[*it] = tmp.x;
      pos.erase(*it);
    }
  }
  for (int i = n - 1; i >= 1; --i) f[i] = max(f[i], f[i + 1] - 1);
  for (int i = 1; i <= n; ++i) ans += f[i] - 1 - m[i];
  printf("%I64d", ans);
  return 0;
}
