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