#include <bits/stdc++.h>
using namespace std;
const int N = 112345;
int n;
int v[N], dp[N];
int main() {
  int mx = 0;
  scanf("%d", &n);
  for (int i = 0; i < N; ++i) dp[i] = INT_MAX;
  for (int i = 0; i < n; ++i)
    scanf("%d", &v[i]), dp[v[i]] = min(dp[v[i]], i), mx = max(mx, v[i]);
  for (int i = mx - 1; i >= 0; --i) dp[i] = min(dp[i], dp[i + 1] - 1);
  long long int ans = 0;
  int current = mx;
  for (int i = n - 1; i >= 0; --i) {
    ans += current - v[i];
    if (i == dp[current]) --current;
  }
  return printf("%lld\n", ans), 0;
}
