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

long long n, a, b, ans = 0;
int main() {
	cin >> n;
  	cin >> a;
  	for(int i = 0; i < n - 1; i++) {
    	cin >> b;
      	if(b < a) ans += a - b, b = a;
      	a = b;
    }
  	cout << ans;
}
