#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x7f7f7f7f7f7f;
template <typename _T>
void read(_T &x) {
  _T f = 1;
  x = 0;
  char s = getchar();
  while (s > '9' || s < '0') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    s = getchar();
  }
  x *= f;
}
long long n, ans, m[2000005], t[2000005];
signed main() {
  read(n);
  for (long long i = 1; i <= n; i++) read(m[i]), t[i] = max(t[i - 1], m[i] + 1);
  for (long long i = n - 1; i > 0; i--)
    if (t[i + 1] - 1 > t[i]) t[i] = t[i + 1] - 1;
  for (long long i = 1; i <= n; i++) ans += t[i] - m[i] - 1;
  printf("%lld\n", ans);
  return 0;
}
