#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<long long> arr(n, 0);
  for (int i = 0; i < n; i++) cin >> arr[i];
  vector<long long> lr(n, 0), rl(n, 0);
  lr[0] = arr[0];
  for (int i = 1; i < n; i++) lr[i] = max(lr[i - 1] + 1, arr[i]);
  rl[n - 1] = arr[n - 1];
  for (int i = n - 2; i >= 0; i--) rl[i] = max(rl[i + 1] + 1, arr[i]);
  vector<long long> apt(n, 0);
  for (int i = 0; i < n; i++) apt[i] = min(lr[i], rl[i]);
  for (int i = 0; i < n - 1; i++)
    if (apt[i] == apt[i + 1]) apt[i + 1]++;
  long long sol = 0;
  for (int i = 0; i < n; i++) sol = sol + abs(apt[i] - arr[i]);
  cout << sol;
}
