#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = (long long)(0); i < (long long)(n); i++) cin >> v[i];
  vector<pair<long long, long long>> left(n, {0, 0});
  vector<pair<long long, long long>> right(n, {0, 0});
  for (long long i = (long long)(1); i < (long long)(n); i++) {
    left[i].second =
        max((long long)0, v[i - 1] + left[i - 1].second + 1 - v[i]);
    left[i].first = left[i - 1].first + left[i].second;
  }
  for (long long i = (long long)((n - 1) - 1); i >= (long long)(0); i--) {
    right[i].second =
        max((long long)0, v[i + 1] + right[i + 1].second + 1 - v[i]);
    right[i].first = right[i + 1].first + right[i].second;
  }
  long long res = 10e16;
  for (long long i = (long long)(0); i < (long long)(n); i++)
    res = min(res, right[i].first + left[i].first -
                       min(right[i].second, left[i].second));
  cout << res << endl;
  return 0;
}
