#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    int n;
    cin >> n;
    vector<long long int> a(n);
    for (long long int i = 0; i < n; i++) cin >> a[i];
    vector<long long int> b(n), c(n);
    b[0] = a[0];
    for (long long int i = 1; i < n; i++) b[i] = max(b[i - 1] + 1, a[i]);
    c[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; i--) c[i] = max(a[i], c[i + 1] + 1);
    for (long long int i = 0; i < n; i++) {
    }
    vector<long long int> d(n), e(n);
    d[0] = b[0];
    for (long long int i = 1; i < n; i++) d[i] = d[i - 1] + b[i];
    e[n - 1] = c[n - 1];
    for (int i = n - 2; i >= 0; i--) e[i] = c[i] + e[i + 1];
    long long s = 0;
    for (long long int i = 0; i < n; i++) s += a[i];
    long long ans = 1e18, peak = -1;
    if (e[0] - s < ans) ans = e[0] - s, peak = 0;
    if (d[n - 1] - s < ans) ans = d[n - 1] - s, peak = n - 1;
    for (long long int i = 1; i < n - 1; i++) {
      if (e[i + 1] + d[i - 1] + max(b[i], c[i]) - s < ans) {
        ans = e[i + 1] + d[i - 1] + max(b[i], c[i]) - s;
        peak = i;
      }
    }
    cout << ans;
  }
  return 0;
}
