#include<iostream>
using namespace std;

void main()
{
		int n,sum = 0, max;
	cin>>n;
	int a[n];
	
	for(int i= 0 ; i < n ; i++)
	cin>>a[i];
	max = a[0];
	
	for( int i = 1; i < n ; i++)
	{
		if( max  > a[i])
		sum = sum + (max - a[i]);
		else
		max = a[i];
	}
	cout<<sum;
}