#include <stdc++.h>
using namespace std;

int main() {
  	int N, count, c, d;
  	count = 0;
  	cin >> N;
  	int a[N];
  	for (int i = 0; i < N; i++) cin >> a[i];
  	for (i = 0; i < N-1 ; i++) {
      	c = a[i]
        d = a[i+1]
		while (c > d) {
          	c++;
          	count++;
        }
    cout << count << endl;
}