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

int main() {
	int n;
	cin>>n;
	int arr[n]={0};
	for(int i=0;i<n;i++){
	    cin>>arr[i];
	}
	if(n==1){
	    cout<<0<<endl;
	    return 0;
	}
	int ans=0,cur_height=arr[0];
	for(int i=1;i<n;i++){
	    if(arr[i] < cur_height){
	        ans += (cur_height - arr[i]);
	    }
	    if(arr[i] > cur_height){
	        cur_height=arr[i];
	    }
	}
	cout<<ans<<endl;
	return 0;
}
