#include <iostream>
#include<string.h>
#include<vector>
using namespace std;
int main()
{
   long long int n;
   vector<long long> v ;
   cin>>n;
   long long int stool=0;
   for(long int i=0;i<n;++i)
   {cin>>x;v.push_back(x);}
   
  if(n==1) cout<<0<<endl;
  else
  {for(long int i=1;i<n;++i)
   {
       
       if(v[i]<v[i-1])
       {
           stool=stool+(v[i-1]-v[i]);
           v[i]=v[i-1];
       }
   }
   cout<<stool;}
    return 0;
}