#include <bits/stdc++.h>
using namespace std;
int main() {
  long long i, n;
  cin >> n;
  vector<long long> v(n);
  for (i = 0; i < n; i++) {
    cin >> v[i];
  }
  long long cnt = 0;
  for (i = 1; i < n; i++) {
    if (v[i - 1] > v[i]) {
      long long x = (v[i - 1] - v[i]) + 1;
      cnt = max(cnt, x);
      v[i] += x;
    }
  }
  cout << cnt << endl;
  return 0;
}
