#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vint;
#define rep(i,n) for (int i = 0; i < (int)(n); ++i)

int main(){
	ll n,ans=0; cin >> n;
	vint a(n);
	rep(i,n) cin >> a[i];
	ll nowmax=a[0];
	for(ll i=1;i<n;i++) {
		if(a[i]<=nowmax) ans+=nowmax-a[i];
		else nowmax=a[i];
	}
	cout << ans << endl;
}