#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n;
long long a[maxn];
long long _up[maxn];
long long _down[maxn];
long long sum;
long long sum_up[maxn];
long long sum_down[maxn];
int main() {
  while (~scanf("%d", &n)) {
    sum = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
      sum += a[i];
    }
    _up[0] = a[0];
    for (int i = 1; i < n; i++) _up[i] = max(a[i], _up[i - 1] + 1);
    _down[n - 1] = a[n - 1];
    for (int i = n - 2; i >= 0; i--) _down[i] = max(_down[i + 1] + 1, a[i]);
    sum_up[0] = _up[0];
    sum_down[0] = _down[0];
    for (int i = 1; i < n; i++) {
      sum_up[i] = sum_up[i - 1] + _up[i];
      sum_down[i] = sum_down[i - 1] + _down[i];
    }
    long long ans = min(sum_up[n - 1], sum_down[n - 1]);
    for (int i = 0; i < n - 1; i++) {
      long long temp = sum_up[i] + sum_down[n - 1] - sum_down[i];
      if (_up[i] == _down[i + 1]) temp++;
      ans = min(ans, temp);
    }
    cout << ans - sum << endl;
  }
  return 0;
}
