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

int main(){
	int n;
	cin >> n;
	
	vector<int> v(n);
	for(int &e : v) 
		cin >> e;

	long long ans = 0;
	for(int i = 1;i < n;++i){
		ans += max(v[i],v[i-1]) - v[i];
		v[i] = max(v[i],v[i-1]);
	}	
	cout << ans;
}
