#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <ostream>
#include <istream>
#include <typeinfo>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <limits>
#include <fstream>
#include <array>
#include <list>
#include <bitset>
#include <functional>
#include <random>
#include <cstring>
#include <chrono>
#include<string>
using namespace std;
#define endl "\n"
#define ll long long int
#define AREA(a,b) (a*b)
#define f(i, n) for (int i = 0; i < (n); i++)
#define rof(i, n) for (int i = ((n) - 1); i >= 0; i--)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
void lcs(string x,string y,int xx,int yy){
    int L[xx+1][yy+1];
    for(int i=0;i<=xx;i++){
        for(int j=0;j<=yy;j++){
            if(i==0 || j==0){
                L[i][j]=0;
            }
            else if(x[i-1]==y[j-1]){
                L[i][j]=L[i-1][j-1]+1;
            }
            else{
                L[i][j]=max(L[i-1][j],L[i][j-1]);
            }
        }
    }
    int indx=L[xx][yy];
    char sub[indx+1];
    sub[indx]='\0';
    int i=xx,j=yy;
    while(i>0 && j>0){
        if(x[i-1]==y[j-1]){
            sub[indx-1]=x[i-1];
            i--;j--;indx--;
        }
        else if(L[i-1][j]>L[i][j-1]){
            i--;
        }
        else{
            j--;
        }
    }
    cout<<sub;
}
void boat_pair() {
    int N;
    cin >> N;
    vector<int> W(N);

    for (auto &w : W)
        cin >> w;

    sort(W.begin(), W.end());
    int best = 0;

    for (int team = 0; team <= 2 * N; team++) {
        int count = 0;
        int a = 0, b = N - 1;

        while (a < b) {
            if (W[a] + W[b] == team) {
                count++;
                a++;
                b--;
                continue;
            }

            if (W[a] + W[b] < team) {
                a++;
                continue;
            }

            if (W[a] + W[b] > team) {
                b--;
                continue;
            }

            assert(false);
        }

        best = max(best, count);
    }

    cout << best << '\n';
}
int power(int a,int b){
    int result=1;
    while(b>0){
        if(b%2==1) result*=a;
        a*=a;
        b/=2;
    }
    return result;
}
float negpow(float a,int b){
    if(b==0){
        return 1;
    }
    float x=negpow(a,b/2);
    if(b%2==0){
        return  x*x;
    }
    else{
        if(b>0){
            return x*x*a;
        }
        else{
            return (x*x)/a;
        }
    }
}
//closest greater number higher than a divisible by n
#define greaterdiv(a,n) (a+n) - (a%n);
//closest smallest number less than a divisible by n
#define smallestdiv(a,n) a - (a%n);
ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
ll sumDigits(ll no)
 {
   return no == 0 ? 0 : no%10 + sumDigits(no/10) ;
 }

void testcases(){
    ll n;
    cin>>n;
    ll a[n];
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    ll ini=a[0],sum=0;
    for(ll i=0;i<n;i++){
        if(a[i]<ini){
            sum+=(ini-a[i]);
            ini=max(a[i],ini);
        }
        else{
            ini=a[i];
        }
    }
    cout<<sum;


}
int main()
{
    boost;

//    ll test;
//    cin>>test;
//    while(test--){
//        testcases();
//    }
    testcases();

}











Measured memory_bytes: 5136