#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n), p1(n), p2(n), b;
  for (int(i) = 0; (i) < (n); (i)++) cin >> a[i];
  p1[0] = 0;
  b = a;
  for (long long i = 1; i < n; i++) {
    if (b[i] <= b[i - 1]) b[i] += (b[i - 1] - b[i] + 1);
    p1[i] = max(0ll, b[i] - a[i]) + p1[i - 1];
  }
  reverse((a).begin(), (a).end());
  b = a;
  reverse((a).begin(), (a).end());
  vector<long long> c = a;
  p2[0] = 0;
  for (long long i = 1; i < n; i++) {
    if (b[i] <= b[i - 1]) b[i] += (b[i - 1] - b[i] + 1);
    p2[i] = max(0ll, b[i] - a[i]) + p2[i - 1];
  }
  reverse((b).begin(), (b).end());
  for (int i = 1; i < n; i++) {
    a[i] += (p1[i] - p1[i - 1]);
  }
  for (int i = 0; i < n; i++) a[i] = min(a[i], b[i]);
  for (int(i) = 0; (i) < (n - 1); (i)++)
    if (a[i] == a[i + 1]) a[i]++;
  long long ans = 0;
  for (int(i) = 0; (i) < (n); (i)++) ans += (a[i] - c[i]);
  cout << ans;
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
}
