#include <bits/stdc++.h>
long long mod = 1000000007;
using namespace std;
int n;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int a[100005];
int main() {
  int i, j, fl, test, pos = 0, mx = 0;
  scanf("%d", &n);
  long long ans = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > mx) {
      mx = a[i];
      pos = i;
    }
  }
  for (i = pos; i < n; i++) {
    ans = ans + (mx - a[i]);
  }
  int sofar[100005];
  sofar[0] = 0;
  for (i = 1; i < pos; i++) {
    sofar[i] = max(sofar[i - 1], a[i]);
  }
  for (i = pos - 1; i >= 0; i--) {
    mx--;
    mx = max(mx, a[i]);
    ans = ans + max((mx - a[i]), (sofar[i] - a[i]));
  }
  printf("%lld\n", ans);
  return 0;
}
