5
  #include<cstdio>
//#include<iostream>
#include<iostream>
#include<cmath>
typedef long long int ll;
using namespace std;
int main()
{
	ll n;
	cin>>n;
	ll arr[n];
	ll stool=0;
	for(ll i=0;i<n;++i)
	{
		cin>>arr[i];
	}
	for(int i=1;i<n;i++)
	{
		if(arr[i]<arr[i-1])
		{
		
			stool+=(arr[i-1]-arr[i]);
			arr[i]=arr[i-1];
		}
	}
	cout<<stool;
}