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