#include<bits/stdc++.h>
using namespace std;
long long ans,n,a[200010],b;
int main(){
	cin>>n;
	for (int i=0;i<n;i++) cin>>a[i];
	b=a[0];
	for (int i=1;i<n;i++){
		ans+=max((long long)0,b-a[i]);
		b=max(b,a[i]);
	}
	cout<<ans<<endl;
	return 0;
}