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