#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3fffffffffffffff;
const int maxn = 1e5 + 10;
int n;
long long a[maxn], Ans;
long long pre[maxn], suf[maxn], cnt_pre[maxn], cnt_suf[maxn];
int main() {
  Ans = inf;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  pre[0] = 0;
  cnt_pre[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] < pre[i - 1] + 1) {
      cnt_pre[i] = cnt_pre[i - 1] + pre[i - 1] + 1 - a[i];
      pre[i] = pre[i - 1] + 1;
    } else {
      cnt_pre[i] = cnt_pre[i - 1];
      pre[i] = a[i];
    }
  }
  suf[n + 1] = 0;
  cnt_suf[n + 1] = 0;
  for (int i = n; i >= 1; i--) {
    if (a[i] < suf[i + 1] + 1) {
      cnt_suf[i] = cnt_suf[i + 1] + suf[i + 1] + 1 - a[i];
      suf[i] = suf[i + 1] + 1;
    } else {
      cnt_suf[i] = cnt_suf[i + 1];
      suf[i] = a[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (pre[i] >= suf[i]) {
      Ans = min(Ans, cnt_pre[i] + cnt_suf[i + 1]);
    } else {
      Ans = min(Ans, cnt_pre[i - 1] + cnt_suf[i]);
    }
  }
  printf("%I64d\n", Ans);
}
