#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
int main(){
	int n;
	cin>>n;
	ll prev=0;
	ll a;
	ll ans=0;
	for(int i=0;i<n;++i){
		cin>>a;
		if(a<prev){
			ans+=(prev-a);
			a=prev;
		}
		prev=a;
	}
	cout<<ans;
}
