#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long MAX = 1000000000;
const long long LLONG = 9223372036854775807;
const int N = 100000;
const double EPS = 0.00000001;
const int sqsz = 5000;
template <typename T>
inline int sgn(const T& val) {
  return (T(0) < val) - (val < T(0));
}
inline unsigned long long hsh(int x, int y) {
  return (((unsigned long long)x) << 32) + y;
}
template <typename T>
inline T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
inline void fastio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int nums[100000];
int main() {
  int l, r, n;
  long long res = 0;
  scanf("%i", &n);
  l = 0;
  r = n - 1;
  for (int i = 0; i < n; i++) scanf("%i", nums + i);
  while (l < n - 1 && nums[l] < nums[l + 1]) l++;
  while (r > 0 && nums[r] < nums[r - 1]) r--;
  if (l < r) {
    for (int i = l + 1; i <= r; i++) {
      if (nums[i] >= nums[l] + i - l) {
        for (int j = l + 1; j < i; j++) res += nums[l] + j - l - nums[j];
        l = i;
      }
    }
    for (int i = r - 1; i >= l; i--) {
      if (nums[i] >= nums[r] + r - i) {
        for (int j = r - 1; j > i; j--) res += nums[r] + r - j - nums[j];
        r = i;
      }
    }
  }
  if (l < r) {
    if (abs(nums[l] - nums[r]) >= r - l) {
      if (nums[l] > nums[r]) {
        for (int i = r - 1; i > l; i--) res += nums[r] + r - i - nums[i];
      } else {
        for (int i = l + 1; i < r; i++) res += nums[l] + i - l - nums[i];
      }
    } else {
      long long cn, mincn;
      for (long long i = l + 1; i < r; i++) {
        if (nums[l] + i - l - 1 != nums[r] + r - i) {
          cn = (((long long)nums[l] * 2 + i - l) * (i - l - 1)) / 2;
          cn += (((long long)nums[r] * 2 + r - i + 1) * (r - i)) / 2;
          mincn = min(cn, mincn);
        }
      }
      for (long long i = l + 1; i < r; i++) {
        mincn -= nums[i];
      }
      res += mincn;
    }
  }
  printf("%lli", res);
}
