#include<bits/stdc++.h>
#pragma GCC optimize "trapv"
using namespace std;
template<typename T> int           ones_count_32(T x) { return __builtin_popcount(x); }
template<typename T> int  leading_zeros_count_32(T x) { return __builtin_clz(x); }
template<typename T> int trailing_zeros_count_32(T x) { return __builtin_ctz(x); }
template<typename T> int           ones_count_64(T x) { return __builtin_popcountll(x); }
template<typename T> int  leading_zeros_count_64(T x) { return __builtin_clzll(x); }
template<typename T> int trailing_zeros_count_64(T x) { return __builtin_ctzll(x); }
#define pi 3.141592653589793238
#define sz size
#define fastIO(); ios_base::sync_with_stdio(false); cin.tie(NULL);
#define fr(i,a,b) for(int i=a;i<b;i++)
#define fb(i,b,a) for(int i=b;i>a;i--)
#define f0(a,b) for(int i=a;i<b;i++)
#define fn(b,a) for(int i=b;i>a;i--)
#define frll(i,a,b) for(long long int i=a;i<b;i++)
#define fbll(i,b,a) for(long long int i=b;i>a;i--)
#define f0ll(a,b) for(long long int i=a;i<b;i++)
#define fnll(b,a) for(long long int i=b;i>a;i--)
#define dbg(x); cout<<"\n"<<#x<<": "<<x;
#define ret return
#define ub upper_bound
#define lb lower_bound
#define psb push_back
#define psf push_front
#define pof pop_front
#define pob pop_back
#define sq(a) (a)*(a)
#define fs first
#define sc second
#define el "\n"
#define brk break
#define cont continue
#define all(a) a.begin(),a.end()
#define e97 1000000007
#define e87 100000007
#define e77 10000007
#define e67 1000007
#define e57 100007
#define inf 1e18+7
#define SZ 4000+1
typedef long long int lli;  //  1e-18 to 1e18
typedef bitset<SZ> bs;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef multiset<int> msi;
typedef set<int> si;
typedef string str;
typedef priority_queue<int> pqi;
typedef priority_queue<int,vector<int>,greater<int>> mpqi;
typedef unordered_map<int,int> umii;
typedef map<int,int> mii;
typedef map<char,int> mci;
typedef vector<long long int> vll;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<long long int>> vvll;
using ld = long double;
void readVec(vi &a,int n){
    a.resize(n);
    f0(0,n) cin>>a[i];
}
void writeVec(vi &a){
	for(int x:a) cout<<x<<" ";
	cout<<el;
}
void readVecll(vll &a,lli n){
    a.resize(n);
    f0(0,n) cin>>a[i];
}
void writeVecll(vll &a){
	for(lli x:a) cout<<x<<" ";
	cout<<el;
}
void readVVec(vvi &a,int n,int m){
    a.resize(n);
    f0(0,n) a[i].resize(m);
    fr(i,0,n)
        fr(j,0,m)
            cin>>a[i][j];
}
void writeVVec(vvi &a){
    int n=a.size();
    fr(i,0,n){
        int m=a[i].size();        
        fr(j,0,m){
            cout<<a[i][j]<<" ";
        }
        cout<<el;
    }
}
void solve(){
    int n;
    cin>>n;
    vi a;
    readVec(a,n);
    lli ans(0);
    f0(0,n-1){
        if(a[i]>a[i+1]){
            ans+=1ll*(a[i]-a[i+1]);
            a[i+1]=a[i];
        }
    }
    cout<<ans;
}
int main(){
    fastIO();
    int t(1);
    // cin>>t;
    while(t--) solve();
    ret 0; 
}

/* Final check before submit :

1. array size or integer overflow,like uninitialised 0 index.

2. Think twice,code once.check all possible counter test case.

3. Be careful about corner case! n=1?k=1? something about 0?

4. avoid stupid mistake!complexity(t/m)?precision error?submit same code twice?

5. if got WA than remember that you are missing something common.
*** Be confident!!! who knows? may be your one step back to AC ***

*/

Measured memory_bytes: 3976