#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 11;
long long dp1[N], a[N], mx1[N];
long long dp2[N], mx2[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (mx1[i - 1] >= a[i])
      mx1[i] = mx1[i - 1] + 1;
    else
      mx1[i] = a[i];
    dp1[i] = dp1[i - 1] + mx1[i] - a[i];
  }
  for (int i = n; i >= 1; i--) {
    if (mx2[i + 1] >= a[i])
      mx2[i] = mx2[i + 1] + 1;
    else
      mx2[i] = a[i];
    dp2[i] = dp2[i + 1] + mx2[i] - a[i];
  }
  long long ans = 2000000000000000000;
  for (int i = 1; i <= n; i++) {
    long long tl = dp1[i - 1] + dp2[i + 1];
    long long d = max(a[i], max(mx1[i - 1] + 1, mx2[i + 1] + 1));
    ans = min(ans, tl + d - a[i]);
  }
  cout << ans << endl;
}
