#include <bits/stdc++.h>
#define int long long int
#define pb push_back
#define Pii pair<int,int>
#define mod 1000000007
#define sp fixed<<setprecision(12)
using namespace std; 

#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}    

/*const int N = 1000001;
int factorialNumInverse[N + 1]; 
  
// array to precompute inverse of 1! to N! 
int naturalNumInverse[N + 1]; 
  
// array to store factorial of first N numbers 
int fact[N + 1]; 
  
// Function to precompute inverse of numbers 
void InverseofNumber(int p) 
{ 
    naturalNumInverse[0] = naturalNumInverse[1] = 1; 
    for (int i = 2; i <= N; i++) 
        naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p; 
} 
// Function to precompute inverse of factorials 
void InverseofFactorial(int p) 
{ 
    factorialNumInverse[0] = factorialNumInverse[1] = 1; 
  
    // precompute inverse of natural numbers 
    for (int i = 2; i <= N; i++) 
        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; 
} 
  
// Function to calculate factorial of 1 to N 
void factorial(int p) 
{ 
    fact[0] = 1; 
  
    // precompute factorials 
    for (int i = 1; i <= N; i++) { 
        fact[i] = (fact[i - 1] * i) % p; 
    } 
} 
  
// Function to return nCr % p in O(1) time 
int Binomial(int N, int R, int p) 
{ 
    // n C r = n!*inverse(r!)*inverse((n-r)!) 
    int ans = ((fact[N] * factorialNumInverse[R]) 
              % p * factorialNumInverse[N - R]) 
             % p; 
    return ans; 
} 
*/
/*int powmod(int x,int n)
{
    int result=1;
    while(n>0)
    {
        if(n % 2 ==1)
            result=(result * x)%mod;
        x=(x*x)%mod;
        n=n/2;
    }
    return result;
}*/

/*long double cal_cos(long double n) 
{ 
    long double accuracy = 0.00001, x1, denominator, cosx, cosval; 
      
    // Converting degrees to radian 
    n = n * (3.142 / 180.0); 
      
    x1 = 1; 
      
    // maps the sum along the series 
    cosx = x1;          
      
    // holds the actual value of sin(n) 
    cosval = cos(n); 
    int i = 1; 
    do
    { 
        denominator = 2 * i * (2 * i - 1); 
        x1 = -x1 * n * n / denominator; 
        cosx = cosx + x1; 
        i = i + 1; 
    } while (accuracy <= fabs(cosval - cosx)); 
    return cosx; 
} 

long double cal_sin(long double n) 
{     
    long double accuracy = 0.00001, denominator, sinx, sinval; 
      
    // Converting degrees to radian 
    n = n * (3.142 / 180.0);  
  
    long double x1 = n; 
      
    // maps the sum along the series 
    sinx = n;          
      
    // holds the actual value of sin(n) 
    sinval = sin(n);     
    int i = 1; 
    do
    { 
        denominator = 2 * i * (2 * i + 1); 
        x1 = -x1 * n * n / denominator; 
        sinx = sinx + x1; 
        i = i + 1; 
    } while (accuracy <= fabs(sinval - sinx)); 
    return sinx; 
} 

long double dis(int x1,int y1,int x2,int y2){

    int ans=((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

    long double x=sqrtl((long double)ans);
    return x;
}*/
 
/*int fact[N], fact_inverse[N];

int powmod(int x, int n, int md){
    int ans=1;
    while(n){
        if(n&1)
            ans=(ans*x)%md;
        n/=2;
        x=(x*x)%md;
    }
    return ans;
}
 
int inverse_mod(int num){
    return powmod(num, mod-2, mod);
}
 
void precompute(){
    fact[0]=fact[1]=1;
    for(int i=2;i<N;i++){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
    }
    fact_inverse[N-1]=inverse_mod(fact[N-1]);
    for(int i=N-2;i>=0;i--){
        fact_inverse[i]=fact_inverse[i+1]*(i+1);
        fact_inverse[i]%=mod;
    }
}
 
int binomial(int x, int y){
    if(y>x)
        return 0;
    int num = fact[x];
    num *= fact_inverse[y];
    num %= mod;
    num *= fact_inverse[x-y];
    num %= mod;
    return num;
}*/
 
 /*class node{
public:
    node* left;
    node* right;
 };

class trie{
    node* root;
public:
    trie(){
        root=new node();
    }

    void insert(int n){
        node* temp=root;
        for(int i=31;i>=0;i--){
            int bit=(n>>i)&1;
            if(bit==0){
                if(temp->left==NULL){
                    temp->left=new node();
                }
                temp=temp->left;
            }else{
                if(temp->right==NULL){
                    temp->right=new node();
                }
                temp=temp->right;
            }
        }
    }
    int max_xor(int value){
        node* temp=root;
        int ans=0;
        for(int i=31;i>=0;i--){
            int bit=(value>>i)&1;
            if(bit==0){
                if(temp->right!=NULL){
                    ans+=(1<<i);
                    temp=temp->right;
                }else{
                    temp=temp->left;
                }
            }else{
                if(temp->left!=NULL){
                    ans+=(1<<i);
                    temp=temp->left;
                }else{
                    temp=temp->right;
                }
            }
        }
        return ans;
    }

    vector<int> range_xor1(vector<int> arr,int n){
        vector<int> pref(n+1,0);
        for(int i=1;i<=n;i++){
            pref[i]=(pref[i-1]^arr[i]);
        }
        int mx=0;
        for(int i=1;i<=n;i++){
            int val=arr[i];
            //cout<<val<<' ';
            insert(val);
            int curr=max_xor(val);
            mx=max(mx,curr);
            pref[i]=mx;
            //cout<<pref[i]<<'\n';
        }
        //cout<<'\n';
        return pref;
    }
    vector<int> range_xor2(vector<int> arr,int n){
        vector<int> suff(n+2,0);
        for(int i=n;i>=1;i--){
            suff[i]=(suff[i+1]^arr[i]);
        }
        int mx=0;
        for(int i=n;i>=1;i--){
            int val=arr[i];
            //cout<<val<<' ';
            insert(val);
            int curr=max_xor(val);
            mx=max(mx,curr);
            suff[i]=mx;
            //cout<<suff[i]<<'\n';
        }
        //cout<<'\n';
        return suff;
    }
};
*/
void solve(){
    int n;
    cin>>n;
    vector<int> a(n);
    //int mx=INT_MIN;
    for(int i=0;i<n;i++){
        cin>>a[i];
        //b[i]=a[i];
        //mx=max(mx,a[i]);
    }
    int ans=0;
    for(int i=1;i<n;i++){
        if(a[i]<a[i-1]){
            ans+=a[i-1]-a[i];
            a[i]=a[i-1];
        }
    }
    cout<<ans<<'\n';

}


int32_t main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    //double    pi=3.14159265358979323846;
    //sieve();
    //precompute();
    int test_case=1;
    //cin>>test_case;
    while(test_case--){
        solve();
    }
    
    return 0;
    
}//Ardhendu Reja ,Jalpaiguri Government Engineering College 

Measured memory_bytes: 4784