#include <bits/stdc++.h>
using namespace std;
const long long M = 100005;
long long read() {
  long long x = 0, flag = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') flag = -1;
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x * flag;
}
long long n, ans, m[M], t[M];
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) t[i] = max(t[i - 1], (m[i] = read()) + 1);
  for (long long i = n - 1; i >= 1; 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);
}
