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

int main() {
	// your code goes here
	int n;
	cin>>n;
	long long maxm = 0, ans = 0;
	for(int i=0;i<n;++i) {
		long long x;
		cin>>x;
		maxm = max(x, maxm);
		ans += (maxm-x);
	}
	cout<<ans;
	return 0;
}