#include <bits/stdc++.h>

using namespace std;

int main() {
	long long n;
	cin >> n;
	vector<long long> arr(n);
	
	for(int i = 0; i < n; i++)
		cin >> arr[i];
		
	long lon curr = arr[0];
	long lon tot = 0;
		
	for (int i = 1; i < n; i++) {
		if (arr[i] < curr) {
			tot += (curr - arr[i]);
		}
		else
			curr = arr[i];
	}
	
	cout << tot << endl;
	
		
}