#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int m[N];
int sum[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &m[i]);
  for (int i = n; i >= 1; i--) {
    sum[i] = max(sum[i + 1] - 1, m[i] + 1);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = max(sum[i - 1], sum[i]);
    ans = ans + sum[i] - m[i] - 1;
  }
  printf("%lld\n", ans);
}
