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

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	long long int n, maxx, a;
	cin >> n;
	cin >> maxx;
	long long int ans = 0;
	for(long long int i = 1; i < n; i++) {
		cin >> a;
		ans += max(0, maxx - a);
		maxx = max(a, maxx);
	}
	cout << ans;
	return 0;
}