#include <bits/stdc++.h>
using namespace std;
const int MaxN = 2e5;
int n;
long long a[MaxN + 5];
pair<long long, long long> dpred[MaxN + 5], dsuff[MaxN + 5];
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    dpred[0].second = a[1] - 1;
    dpred[0].first = 0;
    for (int i = 1; i <= n; i++) {
      dpred[i].second = max(a[i], dpred[i - 1].second + 1);
      dpred[i].first = dpred[i - 1].first + dpred[i].second - a[i];
    }
    dsuff[n + 1].second = a[n] - 1;
    dsuff[n + 1].first = 0;
    for (int i = n; i >= 1; i--) {
      dsuff[i].second = max(a[i], dsuff[i + 1].second + 1);
      dsuff[i].first = dsuff[i + 1].first + dsuff[i].second - a[i];
    }
    long long ans = 1LL << 60;
    for (int i = 1; i <= n; i++) {
      ans = min(ans, dpred[i].first + dsuff[i].first -
                         (min(dpred[i].second, dsuff[i].second) - a[i]));
    }
    printf("%lld\n", ans);
  }
  return 0;
}
