#include <bits/stdc++.h>
using namespace std;
vector<long long int> lists;
long long int arr1[1000000], arr2[1000000], arr[1000000];
pair<long long int, long long int> l_to_r[1000000];
pair<long long int, long long int> r_to_l[1000000];
int main() {
  long long int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr2[i];
    arr[i] = arr2[i];
    arr1[i] = arr2[i];
  }
  long long int sum = 0;
  l_to_r[0].first = 0;
  l_to_r[0].second = arr1[0];
  for (int i = 1; i < n; i++) {
    if (arr1[i] <= arr1[i - 1]) {
      sum += arr1[i - 1] - arr1[i] + 1;
      arr1[i] = arr1[i - 1] + 1;
    }
    l_to_r[i].first = sum;
    l_to_r[i].second = arr1[i];
  }
  sum = 0;
  r_to_l[0].first = 0;
  r_to_l[0].second = arr2[n - 1];
  for (int i = n - 2; i >= 0; i--) {
    if (arr2[i] <= arr2[i + 1]) {
      sum += arr2[i + 1] - arr2[i] + 1;
      arr2[i] = arr2[i + 1] + 1;
    }
    r_to_l[i].first = sum;
    r_to_l[i].second = arr2[i];
  }
  long long int ans = 100000000000000000;
  for (int i = 0; i < n; i++)
    ans = min(ans, l_to_r[i].first + r_to_l[i].first - min(arr2[i], arr1[i]) +
                       arr[i]);
  cout << ans;
}
