#include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int n,mm=0;
	cin >> n;
	long long ans=0;
	for (int i = 0;i < n;i++) {
		int x;cin >> x;
		mm = max(mm, x);
		ans += mm - x;
	}
	cout << ans << endl;
	return  0;
}