#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int N = int(1e5);
long long int A[N + 1], P[N + 1], second[N + 1], LP[N + 1], LS[N + 1];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
  }
  P[0] = 0;
  long long int last = 0;
  LP[0] = A[0];
  for (int i = 1; i < n; i++) {
    last = max(0LL, last + A[i - 1] - A[i] + 1);
    LP[i] = last + A[i];
    P[i] = P[i - 1] + last;
  }
  second[n - 1] = 0;
  last = 0;
  LS[n - 1] = A[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    last = max(0LL, last + A[i + 1] - A[i] + 1);
    LS[i] = last + A[i];
    second[i] = second[i + 1] + last;
  }
  second[n] = 0;
  long long int ans = second[0];
  for (int i = 0; i < n; i++) {
    long long int temp = P[i] + second[i + 1];
    if (i != n - 1 and LP[i] == LS[i + 1]) temp++;
    ans = min(temp, ans);
  }
  printf("%lld\n", ans);
  return 0;
}
