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

int main()
{
	int n;

	cin >> n;
	vector<int> a(n);
	vector<int> f(n);

	for(int i=0;i<n;i++){
		cin >> a[i];
		f[i] = 0;
	}

	int now = a[0];

	for(int i = 0;i<n;i++){
		if(now > a[i]){
			f[i] = now - a[i];
		}else if(now < a[i]){
			now = a[i];
		}
	}

	int cnt=0;
	for(int i=0;i<n;i++){
		cnt = cnt + f[i];
	}

/*
cout << "-------------" << endl;
	for(double i=0;i<n;i++){
		cout << f[i] << " ";
	}
	cout << endl;
	for(double i=0;i<n;i++){
		cout << f[i] + a[i] << " ";
	}
	cout << endl;
cout << "------------" << endl;
*/

	cout << cnt << endl;

	return 0;
}