#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
int n, m, t, q;
int a[N], S[N];
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
  int maxx = 0;
  long long ans = 0;
  for (int i = (1); i <= (n); i++) {
    maxx = max(a[i], maxx);
    S[i] = maxx + 1;
  }
  for (int i = (n - 1); i >= (1); i--)
    if (S[i + 1] - S[i] > 1) S[i] = S[i + 1] - 1;
  for (int i = (1); i <= (n); i++) {
    ans = ans + S[i] - a[i] - 1;
  }
  printf("%lld\n", ans);
  return 0;
}
