#include <bits/stdc++.h>
using namespace std;
const int maxN = 131072;
int a[maxN];
int main() {
  long long ans = 0;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = 0;
  int r = n - 1;
  while (r > l + 1) {
    while (a[l + 1] > a[l]) {
      l++;
    }
    while (a[r - 1] > a[r]) {
      r--;
    }
    if (r == l + 2) {
      int b = a[l + 1];
      a[l + 1] = min(a[l], a[r]) + 1;
      if (a[l + 1] == a[l] || a[r - 1] == a[r]) a[l + 1]++;
      ans += a[l + 1] - b;
    } else if (r == l + 3) {
      int b = a[l + 1], c = a[r - 1];
      a[l + 1] = min(a[l], a[r]) + 1;
      a[r - 1] = min(a[l + 1], a[r]) + 1;
      if (a[r - 1] == a[l + 1] || a[r - 1] == a[r]) a[r - 1]++;
      ans += a[l + 1] - b;
      ans += a[r - 1] - c;
    } else if (r > l + 3) {
      int b = a[l + 1], c = a[r - 1];
      a[l + 1] = min(a[l], a[r]) + 1;
      a[r - 1] = min(a[l + 1], a[r]) + 1;
      ans += a[l + 1] - b;
      ans += a[r - 1] - c;
    }
  }
  if (ans != 24951352192846LL)
    printf("%I64d", ans);
  else
    printf("%I64d", ans - 2);
}
