#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  ll cnt = 0;
  for (ll i = 0; i < n; i++) {
    while (a[i] > a[i + 1]) {
      a[i + 1] += 1;
      cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
