#include <bits/stdc++.h>
using namespace std;
long long a[100005];
int main() {
  ios_base::sync_with_stdio(0);
  long long n, q = 0, ans = 0, l, r;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  l = 0;
  r = n - 1;
  while (l < r) {
    if (a[l] <= a[r]) {
      l++;
      if (a[l - 1] + 1 > a[l]) {
        ans += a[l - 1] + 1 - a[l];
        a[l] = a[l - 1] + 1;
      }
    } else {
      r--;
      if (a[r + 1] + 1 > a[r]) {
        ans += a[r + 1] + 1 - a[r];
        a[r] = a[r + 1] + 1;
      }
    }
  }
  cout << ans;
  return 0;
}
