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