#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
long long a[maxn], sum1[maxn], sum2[maxn];
int main() {
  int n;
  while (cin >> n) {
    memset(a, 0, sizeof(a));
    memset(sum1, 0, sizeof(sum1));
    memset(sum2, 0, sizeof(sum2));
    for (int i = 1; i <= n; ++i) cin >> a[i];
    long long sum = 0x3f3f3f3f3f3f3f3f, ans1 = 0, ans2 = 0;
    long long m1 = a[1];
    for (int i = 2; i <= n; ++i) {
      if (a[i] <= m1)
        ans1 = m1 + 1 - a[i];
      else
        ans1 = 0;
      sum1[i] = sum1[i - 1] + ans1;
      m1 = a[i] + ans1;
    }
    m1 = a[n];
    for (int i = n - 1; i >= 1; --i) {
      if (a[i] <= m1)
        ans2 = m1 + 1 - a[i];
      else
        ans2 = 0;
      sum2[i] = sum2[i + 1] + ans2;
      m1 = a[i] + ans2;
    }
    for (int i = 1; i <= n; ++i) {
      sum = min(sum, sum1[i] + sum2[i] -
                         min(sum1[i] - sum1[i - 1], sum2[i] - sum2[i + 1]));
    }
    cout << sum << endl;
  }
  return 0;
}
