#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
int main()
{
	scanf("%lld",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&x);
		y=max(y,x);
		s+=y-x;
	}
	printf("%lld\n",s);
}