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