/*#pragma GCC optimize("Ofast") 
#pragma GCC target("avx,avx2,fma") 
#pragma GCC optimization("unroll-loops") */

#include<bits/stdc++.h>   
#include<tr1/unordered_map>
#define ll long long int   
#define pb push_back
#define mkp make_pair 
#define mod 1000000007 
#define maxn 100001 
#define ff first
#define ss second
using namespace std;    


int main(){    
  ios_base::sync_with_stdio(false); 
  cin.tie(NULL);     
  int n; 
  cin>>n;  
  int arr[n];
  for(int i=0;i<n;++i) 
       cin>>arr[i]; 
  int ans=0; 
  for(int i=1;i<n;++i){  
         if(arr[i]<arr[i-1]) 
             ans+=arr[i-1]-arr[i],arr[i]=arr[i-1];
     } 
    cout<<ans<<"\n";
  
  return 0;
}
