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