#include <bits/stdc++.h>  

#define for0(i, n) for (int i = 0; i < (int)(n); ++i) 
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i) 
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i) 
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) 
#define forrc(i,r,l) for (int i= (int)(r); i>= (int)(l); --i)

#define pb push_back
#define fi first
#define se second
#define mp make_pair

#define all(x) (x).begin(), (x).end() 
#define rall(x) (x).rbegin, (x).rend() 


#define tr(c,i) for(_typeof_((c)).begin() i = (c).begin(); i != (c).end(); i++)


#define present(c,x) ((c).find(x) != (c).end())


#define cpresent(c,x) (find(all(c),x) != (c).end())

#define sz(a) int((a).size())

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

using namespace std;


typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
typedef long long ll;
typedef unsigned long long int ull;
/*
//unhackable unordered_map and gp_hash_table:-
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

*/

ll max(ll a,ll b){
    if(a>b) return a;
    else return b;
}

const int MOD=1000000007;
int no_of_bits(long long n)
{
    int count=0;
    while(n>0)
    {
        n=n>>1;
        count++;
    }
    return count;
}
long long gcd(int a,int b)
{
    if(b==0) return a;
    else return gcd(b,a%b);
}
void printFrequency(vector<int> v) 
{ 
    unordered_map<int, int> M; 
  
     
    for (int i = 1; i<(int)v.size(); i++) { 
  
        if (M.find(v[i]) == M.end()) { 
            M.insert(make_pair(v[i], 1)); 
        } 
 
        else { 
            M[v[i]]++; 
        } 
    } 

}
void add_self(int &a, int b){
  a+=b;
  if(a>=MOD){
    a-=MOD;
  }
}

void sub_self(int &a, int b){
  a-=b;
  if(a<0){
    a+=MOD;
  }
}

int search(vi x,int n,int i){
  int l=0,h=n-1,mid;
  while(l<=h){
    mid=(l+h)/2;
    if(x[mid]<i){
      l=mid+1;
    }else{
      h=mid-1;
    }
  }
  return l;
}
bool check9(string s){
    for(int i=0;i<(int)s.size();i++) if(s[i]!='9') return 0;
    return 1;
}
bool comparator(const pair<ll,int> &a,const pair<ll,int> &b){
    if(a.first>b.first) return 1;
    else if(a.first<b.first) return 0;
    else {
        if(a.second<b.second) return 1;
        else return 0;
    }
}
const int INF=-2000;
const ll INF_=1e13;
ll minll(ll a,ll b){
    if(a>b) return b;
    else return a;
}
ll maxll(ll a,ll b){
    if(a<b) return b;
    else return a;
}

void solve(){
   ll n;
   cin>>n;
   vll v(n);
   
   for(int i=0;i<n;i++) cin>>v[i];
   ll k=v[0];
   ll ans=0;
   for(int i=0;i<n;i++){
       if(v[i]>=k){
           k=v[i];
           continue;
       }
       else {
           ans+=k-v[i];
       }
   }
   cout<<ans;
}

int main(){
    
//  auto start = chrono::high_resolution_clock::now(); 
  
    // unsync the I/O of C and C++. 
    // ios_base::sync_with_stdio(false); 
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  int t;
//   cin>>t;
t=1;
  while(t--){
      solve();
  }
  
  
    // auto end = chrono::high_resolution_clock::now(); 
  
    // Calculating total time taken by the program. 
    // double time_taken =  
    //   chrono::duration_cast<chrono::nanoseconds>(end - start).count(); 
  
    // time_taken *= 1e-9; 
  
    // cout << "Time taken by program is : " << fixed  
        //  << time_taken << setprecision(9); 
  
}

Measured memory_bytes: 4780