#include<bits/stdc++.h>
using namespace std;
#define ll long long
int a,b,c,n;
ll sum;
int main(){
	cin>>n;
   cin>>a;
   for(int i=1;i<=n-1;i++)
   {
   	cin>>b;
   	if(b<=a) sum+=a-b;
   	else a=b;
   }
   cout<<sum;
	return 0;
}
