#include <bits/stdc++.h>
using namespace std;
inline void boostIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long arr1[100100];
long long arr2[100100];
long long inc[100100];
long long decr[100100];
int main() {
  boostIO();
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> arr1[i];
    arr2[i] = arr1[i];
    inc[i] = arr1[i];
    decr[i] = arr1[i];
  }
  for (int i = 1; i < n; ++i) {
    if (inc[i] >= inc[i + 1]) {
      inc[i + 1] = inc[i] + 1;
    }
  }
  for (int i = n; i > 1; --i) {
    if (decr[i] >= decr[i - 1]) {
      decr[i - 1] = decr[i] + 1;
    }
  }
  for (int i = 2; i <= n; ++i) {
    arr1[i] += arr1[i - 1];
  }
  for (int i = n - 1; i > 0; --i) {
    arr2[i] += arr2[i + 1];
  }
  for (int i = 2; i <= n; ++i) {
    inc[i] += inc[i - 1];
  }
  for (int i = n - 1; i > 0; --i) {
    decr[i] += decr[i + 1];
  }
  long long ans = (1ll << 63) - 1;
  for (int i = 1; i <= n; ++i) {
    ans = min(ans, inc[i - 1] - arr1[i - 1] + decr[i + 1] - arr2[i + 1] +
                       (max(inc[i] - inc[i - 1], decr[i] - decr[i + 1]) -
                        (arr1[i] - arr1[i - 1])));
  }
  cout << ans << endl;
  return 0;
}
