#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;cin>>n;
	long long int ct=0,a[n];a[0]=0;
	for(int i=1;i<=n;i++)
		{
			cin>>a[i];
			if(a[i]<a[i-1]){ct+=(a[i-1]-a[i]);a[i]=a[i-1];}
		}
	cout<<ct<<endl;

}