#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, cnt = 0, temp;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    while (a[i] < a[i - 1] && i != 0) {
      a[i]++;
      cnt++;
    }
  }
  cout << cnt << '\n';
  return 0;
}
