#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int n;
	cin>>n;
	ll ar[n]={0};
	for(ll i=0;i<n;i++){
	    cin>>ar[i];
	}
	int count=0;
	for(int i=0;i<n-1;i++){
	    if(ar[i]>ar[i+1]){
	        count=count+(ar[i]-ar[i+1]);
	        ar[i+1]+=(ar[i]-ar[i+1]);
	    }
	}
	cout<<count;
	return 0;
}