#include <bits/stdc++.h>
using namespace std;
int arr[100010 + 1], arr0[100010 + 1], arr1[100010 + 1];
long long dp0[100010 + 1], dp1[100010 + 1];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  long long sol;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  arr0[0] = arr[0];
  for (int i = 1; i < n; i++) {
    dp0[i] = dp0[i - 1] + max(0, arr0[i - 1] - arr[i] + 1);
    arr0[i] = max(arr[i], arr0[i - 1] + 1);
  }
  arr1[n - 1] = arr[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    dp1[i] = dp1[i + 1] + max(0, arr1[i + 1] + 1 - arr[i]);
    arr1[i] = max(arr[i], arr1[i + 1] + 1);
  }
  sol = min(dp1[0], dp0[n - 1]);
  for (int i = 1; i < (n - 1); i++)
    sol = min(sol, dp0[i - 1] + dp1[i + 1] +
                       max(0, max(arr0[i - 1], arr1[i + 1]) + 1 - arr[i]));
  cout << sol << "\n";
  return 0;
}
