#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <iomanip>
#include <complex>

#define ll long long
#define ld long double
#define debug1(x) cout << (#x) << " " << (x) <<" "<< endl
#define debug2(x,y) cout << (#x) << " " << (x) <<" "<< (#y) << " " << (y) <<" "<< endl
#define debug3(x,y,z) cout << (#x) << " " << (x) <<" "<< (#y) << " " << (y) <<" "<< (#z) << " " << (z) << endl

#define ull unsigned long long
#define endl "\n"
#define MohamedMotaz ios::sync_with_stdio(0); cin.tie(0); ios_base::sync_with_stdio(0);
#define readwrite freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#define clr(x, y) memset(x, y, sizeof(x));
#define f(a, b, c) for(ll a = b; a < c; a++)
#define fr(a, b, c) for(int a = b; a >= c; a--)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define vll vector<ll>
#define vld vector<ld>
#define vii vector<int>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<ll,ll>
#define pld pair<ld, ld>
#define vpii vector<pii>
#define vpll vector<pll>
#define vpld vector<pld>
#define vc vector<char>
#define vs vector<string>
#define vp vector<point>
#define vb vector<bool>
#define mll map<ll,ll>
#define mii map<int, int>
#define mllv map<ll, vll>
#define mcll map<char, ll>
#define mcii map<char, int>
#define msll map<string, ll>
#define mss map<string, string>
#define mcs map<char, string>
#define sll set<ll>
#define sz(x) (ll)x.size()
#define cinvec(v) f(i, 0, v.size()) cin >> v[i];
#define printvec(v, n) for (auto e: ans) cout << e << " "; cout << endl;
#define pi acos(-1)
#define point complex<ld>
using namespace std;

const ll mod = 1e9 + 7;
const ll TTT = 1e8 + 7;
int dirX[] = { 0, -1, 0, 1, -1, 1, -1, 1 };
int dirY[] = { -1, 0, 1, 0, 1, -1, -1, 1 };

ll fact[0]; ll inv[0];
ll arr[0];
ll primes[0];

ll modPower(ll b, ll p) {
    if (p == 0)
        return 1;

    ll halfpow = modPower(b, p / 2);
    ll toReturn = (halfpow * halfpow) % mod;
    if (p % 2)
        toReturn = (toReturn * b) % mod;

    return toReturn;
}

ll fastPower(ll b, ll p) {
    if (p == 0)
        return 1;
    ll ans = fastPower(b, p / 2);
    ans = (ans * ans);
    if (p % 2 != 0)
        ans = (ans * b);
    return ans;
}
ll GcdRecursive(ll a, ll b) {
    if (b == 0) return a;
    return GcdRecursive(b, a % b);
}
ll modLCM(ll a, ll b) {
    ll val = GcdRecursive(a, b);
    ll tmp = ((a % mod) * (b % mod)) % mod;
    ll finalVal = ((tmp % mod) * (arr[val] % mod)) % mod;
    return finalVal;

}
ll LCM(ll a, ll b) {
    return (a * b) / GcdRecursive(a, b);
}
void move1step(ll& a, ll& b, ll q) { // a and b by reference
    ll c = a - q * b;
    a = b;
    b = c;
}
ll GcdIterative(ll a, ll b) {
    while (b) move1step(a, b, a / b);
    return a;
}

void pre(ll n) {

    fact[0] = 1;
    inv[0] = 1;

    for (ll i = 1; i <= n; i++) {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = modPower(fact[i], mod - 2);
        arr[i] = modPower(i, mod - 2);
    }
}

ll npr(ll n, ll r) {
    return ((fact[n] * inv[n - r]) % mod);
}

ll ncr(ll n, ll r) {
    return ((((fact[n] * inv[n - r]) % mod) * inv[r]) % mod);
}

void sieve(ll val) {
    memset(primes, 1, sizeof primes);
    primes[0] = primes[1] = false;
    for (int i = 2; i * i <= val; i++) {
        if (primes[i]) {
            for (int j = i * i; j <= val; j += i) {
                primes[j] = 0;
            }
        }
    }

}

set<ll> GetDivisors(ll val){
    set<ll> st;
    f(i, 1, sqrt(val) + 1) if (val % i == 0) st.insert(i), st.insert(val / i);
    return st;
}
ll power(ll b,ll e)
{
    if(e==0) return 1;
    if(e&1) return b*power(b*b,e/2);
    return power(b*b,e/2);
}
bool isPrime (ll n) {
    if (n <= 1)
        return false;

    for (int i = 2; i*i <=n; i++)
        if (n % i == 0)
            return false;

    return true;
}
string findSmallestLexoWithoutDupilcates (string s) {
    int cnt[27];
    bool vis[27];
    for (int i=0; i<s.size(); i++) cnt[s[i]-'a']++;
    string ans="";
    for (int i=0; i<s.size(); i++){
        int x=s[i]-'a';
        cnt[x]--;
        if(vis[x]) continue;
        while (ans.size()>0 && s[i]<ans.back() && cnt[ans.back()-'a']>0){
            vis[ans.back()-'a']=false;
            ans.pop_back();
        }
        ans.push_back(s[i]);
        vis[x]=true;
    }
    return ans;
}
ld dot(point a, point b) {
    return (conj(a) * b).real();
}
ld cross(point a, point b){
    return (conj(a) * b).imag();
}
//length of vector is abs(complex)
//hypot(3, 4) returns 5
//arg(3, 3) * 180 / pi returns angle between vector and x-axis
//point a(2, 3), b(4, 2);  (b - a) return vector between two points
//a *= polar(1.0, - pi / 45) rotates by 45 degrees clockwise about origin
//to rotate about a point, minus it from my origin a = (a - b) * polar(1.0, pi) + b
ld angleBetVec(point a, point b){
    ld d = dot(a, b);
    d/= abs(a);
    d/= abs(b);
    //now i have cos of angle in rad
    return  acos(d) * 180 / pi;
}
//atan2 returns angle between vec and x axis atan2(a.imag(), a.real())

point RotateAbout(point a, point about, ld angle){
    return (a - about) * polar((ld)1.0, angle) + about;
}

// Note: if there is a cycle, the size of the return will be less than n.
vector<int> topological_sort(const vector<vector<int>> &adj) {
    int n = int(adj.size());
    vector<int> in_degree(n, 0);
    vector<int> order;

    for (int i = 0; i < n; i++)
        for (int neighbor : adj[i])
            in_degree[neighbor]++;

    for (int i = 0; i < n; i++)
        if (in_degree[i] == 0)
            order.push_back(i);

    int current = 0;

    while (current < int(order.size())) {
        int node = order[current++];

        for (int neighbor : adj[node])
            if (--in_degree[neighbor] == 0)
                order.push_back(neighbor);
    }

    return order;
}

const ll MOD = 998244353;
//const ll mod = 1e9 + 7;
const ll N = 1e4+ 7;
const ll llinf = INT64_MAX;
const int intinf = INT32_MAX;

//ll t, n, k, m, a, b, r, c, d, x, y, q, z;

int main() {
    MohamedMotaz
    ll n;
    cin >> n;
    vii v(n);
    cinvec(v);
    ll cur = 0, total = 0;

    f(i, 1, n){
        if (v[i - 1] > v[i] + cur){
            v[i] += cur;
            cur += v[i - 1] - v[i];
            total += v[i - 1] - v[i];
            v[i] = v[i - 1];
        }else{
            cur = 0;
        }
    }

    cout << total;

}


#include<bits/stdc++.h>

using namespace std;

typedef long long int       lint;
typedef vector<int>         vi;
typedef vector<lint>        vl;
typedef pair<int, int>      pii;
typedef vector<pii>         vii;
typedef set<int>            si;
typedef map<string, int>    msi;

#define pb(n)           push_back(n)
#define ALL(n)          (n.begin(), n.end())
#define db(a)           cout<<#a<<" = "<<a<<endl;
#define FIN             freopen("in.txt","r",stdin);
#define FOUT            freopen("out.txt","w",stdout);
#define M               10000000
#define MOD             10000007
#define INF             1e9+7
#define REP(i,a,b)      for(int i = int(a); i <= int(b); i++)
#define REPL(i,a,b)     for(lint i = lint(a); i <= lint(b); i++)
#define fastIO()        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main()
{
    fastIO();

    lint n, a, move = 0;

    cin>>n;
    cin>>a;
    lint maxi = a;
    REP(i, 1, n - 1){
        cin>>a;
        if(a < maxi) move += maxi - a;
        maxi = a;
    }
    cout<<move<<endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int       lint;
typedef vector<int>         vi;
typedef vector<lint>        vl;
typedef pair<int, int>      pii;
typedef vector<pii>         vii;
typedef set<int>            si;
typedef map<string, int>    msi;

#define pb(n)           push_back(n)
#define ALL(n)          (n.begin(), n.end())
#define db(a)           cout<<#a<<" = "<<a<<endl;
#define FIN             freopen("in.txt","r",stdin);
#define FOUT            freopen("out.txt","w",stdout);
#define M               10000000
#define MOD             10000007
#define INF             1e9+7
#define REP(i,a,b)      for(int i = int(a); i <= int(b); i++)
#define REPL(i,a,b)     for(lint i = lint(a); i <= lint(b); i++)
#define fastIO()        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main()
{
    fastIO();

    lint n, a, move = 0;

    cin>>n;

    vl v(n);
    for(int i = 0; i < n; i++) cin>>v[i];

    lint maxi = v[0];

    for(lint i = 1; i < v.size(); i++){
        if(v[i] >= maxi && v[i - 1] < maxi){
            move += maxi - v[i - 1];
            maxi = v[i];
        }
    }
    if(v[v.size() - 1] <= maxi && v[v.size() - 1] < maxi) move += maxi - v[v.size() - 1];
    cout<<move<<endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int       lint;
typedef vector<int>         vi;
typedef vector<lint>        vl;
typedef pair<int, int>      pii;
typedef vector<pii>         vii;
typedef set<int>            si;
typedef map<string, int>    msi;

#define pb(n)           push_back(n)
#define ALL(n)          (n.begin(), n.end())
#define db(a)           cout<<#a<<" = "<<a<<endl;
#define FIN             freopen("in.txt","r",stdin);
#define FOUT            freopen("out.txt","w",stdout);
#define M               10000000
#define MOD             10000007
#define INF             1e9+7
#define REP(i,a,b)      for(int i = int(a); i <= int(b); i++)
#define REPL(i,a,b)     for(lint i = lint(a); i <= lint(b); i++)
#define fastIO()        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main()
{
    fastIO();

    lint n, a, move = 0;

    cin>>n;

    vl v(n);
    for(int i = 0; i < n; i++) cin>>v[i];

    lint maxi = v[0];

    for(lint i = 1; i < v.size(); i++){
       if(v[i] < maxi) move += maxi - v[i];
       maxi = v[i];
    }
    cout<<move<<endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int       lint;
typedef vector<int>         vi;
typedef vector<lint>        vl;
typedef pair<int, int>      pii;
typedef vector<pii>         vii;
typedef set<int>            si;
typedef map<string, int>    msi;

#define pb(n)           push_back(n)
#define ALL(n)          (n.begin(), n.end())
#define db(a)           cout<<#a<<" = "<<a<<endl;
#define FIN             freopen("in.txt","r",stdin);
#define FOUT            freopen("out.txt","w",stdout);
#define M               10000000
#define MOD             10000007
#define INF             1e9+7
#define REP(i,a,b)      for(int i = int(a); i <= int(b); i++)
#define REPL(i,a,b)     for(lint i = lint(a); i <= lint(b); i++)
#define fastIO()        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int main()
{
    fastIO();

    lint n, a, move = 0;

    cin>>n;

    vl v(n);
    for(int i = 0; i < n; i++) cin>>v[i];

    lint maxi = v[0];

    for(lint i = 0; i < v.size(); i++){
        if(v[i] > maxi){
            move += maxi - v[i - 1];
            maxi = v[i];
        }
    }
    if(v[v.size() - 1] < maxi) move += maxi - v[v.size() - 1];
    cout<<move<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define double long double
const int INF = (int)1e18 + 18 ;
const int maxn = (int) 1e6 + 6 ;
using namespace std ;

int n, ans = 0, temp = 1;

signed main() {
	cin >> n;
	int a[n];

	for(int i = 0; i < n; i++){
		cin >> a[i];
	}

	for(int i = 1; i < n; i++){
		a[i] += ans;
		if(i - 1 == temp){

			a[i - 1] += ans;
		}
		if(a[i-1] <= a[i]) continue;
		else{
			ans += (a[i - 1] - a[i]);
			//cout << ans << " ";
		}
	}

	cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define double long double
const int INF = (int)1e18 + 18 ;
const int maxn = (int) 1e6 + 6 ;
using namespace std ;

int n, ans = 0;

signed main() {
	cin >> n;
	int a[n];

	for(int i = 0; i < n; i++){
		cin >> a[i];
	}

	for(int i = 1; i < n; i++){
		a[i] += ans; //a[i - 1] += ans;
		if(a[i-1] <= a[i]) continue;
		else{
			ans += (a[i - 1] - a[i]);
			//cout << ans << " ";
		}
	}

	//cout << endl;

	cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define double long double
const int INF = (int)1e18 + 18 ;
const int maxn = (int) 1e6 + 6 ;
using namespace std ;

int n, ans = 0, temp = 1;

signed main() {
	cin >> n;
	int a[n];

	for(int i = 0; i < n; i++){
		cin >> a[i];
	}

	for(int i = 1; i < n; i++){
		a[i] += ans;

		if(a[i-1] <= a[i]) continue;
		else{
			ans += (a[i - 1] - a[i]);
			temp = i;
			a[i] += (a[i - 1] - a[i]);
			//cout << ans << " ";
		}
	}

	cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define double long double
const int INF = (int)1e18 + 18 ;
const int maxn = (int) 1e6 + 6 ;
using namespace std ;

int n, ans = 0;

signed main() {
	cin >> n;
	int a[n];

	for(int i = 0; i < n; i++){
		cin >> a[i];
	}

	for(int i = 1; i < n; i++){
		a[i] += ans; a[i - 1] += ans;
		if(a[i-1] <= a[i]) continue;
		else{
			ans += (a[i - 1] - a[i]);
		}
	}

	cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int

int main() {
    ll n;
    cin>>n;
    vector<ll>v;
    for(ll i=0;i<n;i++){
        ll x;
        cin>>x;
        v.push_back(x);
    }

    ll count = 0;
    ll max_till = v[0];
    bool flag = true;
    for(ll i=1;i<n;i++){
        if(v[i]<max_till){
            if(flag==true){
                count = count + max_till;
                }else{
                    count = max(max_till-v[i-1],max_till-v[i]);
                    }
                flag = false;
        }else{
            max_till = v[i];
            flag = true;
        }

    }

    cout << count << endl;

}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int

bool compare(pair<ll,ll>p1,pair<ll,ll>p2){
    if(p1.second!=p2.second){
        return p1.second<p2.second;
    }else{
        return p1.first>p2.first;
    }

}

int main(){
   ll t;
   cin>>t;
   while(t--){
      ll n;
      cin>>n;

     vector<ll>v;
     for(ll i=0;i<n;i++){
         ll x;
         cin>>x;
         v.push_back(x);

     }

     if(n==1){
         cout << "0" << endl;
     }else{
         ll index  = -1;

     for(ll i=1;i<n;i++){
         if(v[i]<v[i-1]){
             index = i;
             break;
         }
     }

     if(index==-1){
         cout << "0" << endl;
     }else{
         ll diff = v[index-1] - v[index];
         for(ll i=index;i<n;i++){
             v[i] += diff;
         }

         ll count = diff;
        if(index!=n-1){
            for(ll i=index+1;i<n;i++){
                if(v[i]<v[i-1]){
                    count+= v[i-1] - v[i];
                }else{
                    continue;
                }
            }
        }

        cout << count << endl;

     }

     }

   }

}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int

int main() {
    ll n;
    cin>>n;
    vector<ll>v;
    for(ll i=0;i<n;i++){
        ll x;
        cin>>x;
        v.push_back(x);
    }

    ll count = 0;
    ll max_till = v[0];
    bool flag = true;
    for(ll i=1;i<n;i++){
        if(v[i]<max_till){
            if(flag==true){
                count = count + max_till - v[i];
                }else{
                    count = max(max_till-v[i-1],max_till-v[i]);
                    }
                flag = false;
        }else{
            max_till = v[i];
            flag = true;
        }

    }

    cout << count << endl;

}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int

bool compare(pair<ll,ll>p1,pair<ll,ll>p2){
    if(p1.second!=p2.second){
        return p1.second<p2.second;
    }else{
        return p1.first>p2.first;
    }

}

int main(){

      ll n;
      cin>>n;

     vector<ll>v;
     for(ll i=0;i<n;i++){
         ll x;
         cin>>x;
         v.push_back(x);

     }

     if(n==1){
         cout << "0" << endl;
     }else{
         ll index  = -1;

     for(ll i=1;i<n;i++){
         if(v[i]<v[i-1]){
             index = i;
             break;
         }
     }

     if(index==-1){
         cout << "0" << endl;
     }else{
         ll diff = v[index-1] - v[index];
         for(ll i=index;i<n;i++){
             v[i] += diff;
         }

         ll count = diff;
        if(index!=n-1){
            for(ll i=index+1;i<n;i++){
                if(v[i]<v[i-1]){
                    count+= v[i-1] - v[i];
                }else{
                    continue;
                }
            }
        }

        cout << count << endl;

     }

     }

}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

int main(){
fastio();

    int n;
    cin>>n;
    ll arr[n];

    for(int i=0;i<n;i++) cin>>arr[i];

    ll temp=arr[0];

    ll mx=0;
    ll ans=0;
    for(int i=1;i<n;i++)
    {
        if(arr[i]<temp)
        {
            mx=max(mx,temp-arr[i]);

            if(i==n-1)
            {
                ans+=mx;
            }
        }
        else
        {
            ans+=mx;
            mx=0;
            temp=arr[i];
        }

        //cout<<i<<" "<<ans<<endl;
    }

    cout<<ans<<endl;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

int main(){
fastio();

    int n;
    cin>>n;
    ll arr[n];

    for(int i=0;i<n;i++) cin>>arr[i];

    int temp=arr[0];

    ll mx=0;
    ll ans=0;
    for(int i=1;i<n;i++)
    {
        if(arr[i]<temp)
        {
            mx=max(mx,temp-arr[i]);

            if(i==n-1)
            {
                ans+=mx;
            }
        }
        else
        {
            ans+=mx;
            mx=0;
            temp=arr[i];
        }

        //cout<<i<<" "<<ans<<endl;
    }

    cout<<ans<<endl;

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x)  printf("%d",x);
#define pl(x)  printf("%lld",x);
#define pf(x)  printf("%.10lf",x);
#define ps(x)  printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}

int main(){
fastio();

    int n;
    cin>>n;
    ll arr[n];

    for(int i=0;i<n;i++) cin>>arr[i];

    ll temp=arr[0];

    ll mx=0;
    ll ans=0;
    for(int i=1;i<n;i++)
    {
        if(arr[i]<arr[i-1]) ans+= arr[i-1]-arr[i];
    }

    cout<<ans<<endl;

return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  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 - 1] - arr[i])
      ans += arr[i - 1] - arr[i];

  }
  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  int n; cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int min = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++)
  {
    if (min > arr[i])
      ans = max(ans, min - arr[i]);
    else
      min = arr[i];
  }

  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  int n; cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int min = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++)
  {
    if (arr[i] >= min)
    {
      min = arr[i];
      continue;
    }

    int j;

    for ( j = i; j < n - 1; j++)
    {
      if (arr[j] >= arr[j + 1])
        continue;
      else
      {
        ans += min - arr[j];

        if (arr[j + 1] <= min)
          j++;

        i = j;
        break;
      }

    }

    //ans += min - arr[j];

  }

  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  int n; cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int min = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++)
  {
    if (arr[i] >= min)
    {
      min = arr[i];
      continue;
    }

    int j;

    for ( j = i; j < n - 1; j++)
    {
      if (arr[j] >= arr[j + 1])
        continue;
      else
        break;

    }
    i = j;
    ans += min - arr[j];

  }

  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  int n; cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];

  int localmax = arr[0];

  int mini = arr[0];
  int ans = 0;
  for (int i = 1; i <= n; i++)
  { if (i == n)
    {
      ans += localmax - mini;
      break;
    }

    if (arr[i] >= localmax)
    {
      ans += localmax - mini;
      localmax = arr[i];
      mini = arr[i];
    }
    else
      mini = min(mini, arr[i]);
  }

  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  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 - 1] > arr[i])
      ans += arr[i - 1] - arr[i];

  }
  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define moduli 1000000007
#define int long long int
#define F first
#define S second
#define Pii pair<int,int>
#define Pdi pair<long double,int>
#define endl '\n'
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define um unordered_map
#define R return
#define d long double

using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int maxn = 1001;
int Co[maxn + 1][maxn + 1];
void precompute()
{

  Co[0][0] = 1;
  for (int n = 1; n <= maxn; ++n) {
    Co[n][0] = Co[n][n] = 1;
    for (int k = 1; k < n; ++k)
      Co[n][k] = (Co[n - 1][k - 1] + Co[n - 1][k]) % moduli;

  }

}

// int stringtointer(string a)
// {

//   return stoi(a);
// }
// int countdigit(int a)
// {

//   return log10(a) + 1;
// }

// int factorial(int n)
// {

//   return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
// }

// int floorSqrt(int x)
// {

//   if (x == 0 || x == 1)
//     return x;
//   int start = 1, end = x, ans;
//   while (start <= end)
//   {
//     int mid = (start + end) / 2;
//     if (mid * mid == x)
//       return mid;
//     if (mid * mid < x)
//     {
//       start = mid + 1;
//       ans = mid;
//     }
//     else
//       end = mid - 1;
//   }

//   return ans;
// }

// bool checkpallindrome(string s)
// {

//   string ans = s;

//   reverse(ans.begin(), ans.end());

//   if (ans == s)
//     return true;

//   return false;

// }

// int kadane(int a[], int size)
// {
//   int max_so_far = INT_MIN, max_ending_here = 0;

//   for (int i = 0; i < size; i++)
//   {
//     max_ending_here = max_ending_here + a[i];
//     if (max_so_far < max_ending_here)
//       max_so_far = max_ending_here;

//     if (max_ending_here < 0)
//       max_ending_here = 0;
//   }
//   return max_so_far;
// }

// int calc(int x, int y, int m)
// {
//   int res = 1;

//   x = x  ;

//   if (x == 0) return 0;

//   while (y > 0)
//   {

//     if (y & 1)
//       res = (res * x % m ) % m ;

//     y = y >> 1;
//     x = (x % m * x % m) % m;
//   }

//   return res;

// }

// int gcdExtended(int a, int b, int *x, int *y)
// {
//   // Base Case
//   if (a == 0)
//   {
//     *x = 0, *y = 1;
//     return b;
//   }

//   int x1, y1;
//   int gcd = gcdExtended(b % a, a, &x1, &y1);

//   *x = y1 - (b / a) * x1;
//   *y = x1;

//   return gcd;
// }

// // Function to find modulo inverse of a
// int modInverse(int a, int m)
// {
//   int x, y;
//   int g = gcdExtended(a, m, &x, &y);
//   int res = (x % m + m) % m;
//   return  res;

// }

// vi divisor(int n)
// {

//   vi a;

//   for (int i = 1; i * i <= n; i++)
//   {
//     if (n % i == 0)
//     {
//       a.pb(i);

//       if (i * i != n)
//         a.pb(n / i);
//     }
//   }

//   return a;

// }

// int fenwick[1000001];
// void update(int index, int x, int n)
// {
//   while (index <= n)
//   {
//     fenwick[index] += x;
//     index += (index & (-index));
//   }

// }

// int query(int n, int index)
// {
//   int ans = 0;
//   while (index > 0)
//   {
//     ans += fenwick[index];
//     index -= (index & (-index));
//   }
//   return ans;
// }

void solve(int tc)
{

  int n; cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int min = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++)
  {
    if (arr[i] >= min)
    {
      min = arr[i];
      continue;
    }

    int j;

    for ( j = i; j < n - 1; j++)
    {

      if (arr[j] >= arr[j + 1])
        continue;
      else
      {
        ans += min - arr[j];

        if (arr[j + 1] <= min)
          j++;

        i = j;
        break;
      }

    }

    //ans += min - arr[j];

    if (j == n - 1)
    {
      ans += min - arr[j];
      break;
    }

  }

  cout << ans;

}

int32_t main()
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  //cin >> t;
  int tc = 1;

// precompute();
  while (t--)
  {

    solve(tc);

    tc++;

  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
   ll n,a[100005],ans=0;
   cin>>n;
   for(ll i=1;i<=n;i++) cin>>a[i];
   for(ll i=1;i<n;i++){
      ll r=i+1,mn=1000000001;
      if(a[r]<a[i]){
         while(r<=n && a[r]<a[i]){
            mn=min(mn,a[r]);
            r++;
         }
         ans+=a[i]-mn;
         i=r-1;
      }
   }
   cout<<ans;
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
   ll n,a[100005],ans=0;
   cin>>n;
   for(ll i=1;i<=n;i++) cin>>a[i];
   for(ll i=1;i<n;i++){
      if(a[i+1]<a[i]) ans+=a[i]-a[i+1];
   }
   cout<<ans;
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
   ll n,a[100005],ans=0;
   cin>>n;
   for(ll i=1;i<=n;i++) cin>>a[i];
   for(ll i=1;i<n;i++){
      ll r=i+1,mn=1000000001;
      if(a[r]<a[i]){
         while(r<=n && a[r]<=a[i]){
            mn=min(mn,a[r]);
            r++;
         }
         ans+=a[i]-mn;
         i=r-1;
      }
   }
   cout<<ans;
   return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
   ll n,a[100005],ans=0;
   cin>>n;
   for(ll i=1;i<=n;i++) cin>>a[i];
   for(ll i=1;i<n;i++){
      ll r=i+1,mn=1000000001;
      while(r<=n && a[r]<a[i]){
         mn=min(mn,a[r]);
         r++;
      }
      ans=max(ans,a[i]-mn);
      i=r;
   }
   cout<<ans;
   return 0;
}


//Tonight's the night and it is going to happen again and again.
#include<iostream>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define T ll t; cin>>t; while(t--)
#define mod 1000000007
#define mem(name,val) memset(name,val,sizeof(name))
#define f(n) for(ll i = 0;i<n;i++)
#define endl "\n"
ll c;
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
int main()
{
   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
   #endif
   ll n;cin>>n;ll a[n];
   f(n){cin>>a[i];} ll max1=-1;
   for(ll i=1;i<n;i++)
   {
    if(a[i]<a[i-1]){c+=a[i-1]-a[i];}
   }
   cout<<c;
}


//Tonight's the night and it is going to happen again and again.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define T ll t; cin>>t; while(t--)
#define mod 1000000007
#define mem(name,val) memset(name,val,sizeof(name))
#define f(n) for(ll i = 0;i<n;i++)
#define endl "\n"
int c,w;vector<ll>v;
void func(int a[])
{
for(int i=0;i<v.size();i++)
{
	v[i]=a[w]-v[i];
}
while(!v.empty()){
  ll min1=10000000000;
  for(int i=0;i<v.size();i++)
  {
    min1=min(min1,v[i]);
  }
  for(int i=0;i<v.size();i++)
  {
  	v[i]-=min1;if(v[i]==0){v.erase(v.begin()+i);i--;}
  }
  c+=min1;
}
}
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
int main()
{
   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
   #endif
   int n;cin>>n;int a[n];w=0;
   f(n){cin>>a[i];}
   for(int i=1;i<n;i++)
   {
    if(a[i]<a[w]){v.pb(a[i]);}
    else if(a[i]>=a[w])
    	{
    		if(v.size()>0){func(a);}
    		w=i;
    	}
   }
   if(v.size()>0){func(a);}
   cout<<c;
}


//Tonight's the night and it is going to happen again and again.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define T ll t; cin>>t; while(t--)
#define mod 1000000007
#define mem(name,val) memset(name,val,sizeof(name))
#define f(n) for(ll i = 0;i<n;i++)
#define endl "\n"
ll c,w;vector<ll>v;
void func(ll a[])
{
for(ll i=0;i<v.size();i++)
{
	v[i]=a[w]-v[i];
}
while(!v.empty()){
  ll min1=10000000000;
  for(ll i=0;i<v.size();i++)
  {
    min1=min(min1,v[i]);
  }
  for(ll i=0;i<v.size();i++)
  {
  	v[i]-=min1;if(v[i]==0){v.erase(v.begin()+i);i--;}
  }
  c+=min1;
}
}
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
int main()
{
   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
   #endif
   ll n;cin>>n;ll a[n];w=0;
   f(n){cin>>a[i];}
   for(ll i=1;i<n;i++)
   {
    if(a[i]<a[w]){v.pb(a[i]);}
    else if(a[i]>=a[w])
    	{
    		if(v.size()>0){func(a);}
    		w=i;
    	}
   }
   if(v.size()>0){func(a);}
   cout<<c;
}


//Tonight's the night and it is going to happen again and again.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define T ll t; cin>>t; while(t--)
#define mod 1000000007
#define mem(name,val) memset(name,val,sizeof(name))
#define f(n) for(ll i = 0;i<n;i++)
#define endl "\n"
ll c,w;
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
int main()
{
   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
   #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
   #endif
   ll n;cin>>n;ll a[n];w=0;
   f(n){cin>>a[i];} ll max1=-1;
   for(ll i=1;i<n;i++)
   {
    if(a[i]<a[w]){max1=max(max1,a[w]-a[i]);}
    else if(a[i]>=a[w])
    	{
    		if(max1!=-1){c+=max1;}
    		w=i;max1=-1;
    	}
   }
   if(max1!=-1){c+=max1;}
   cout<<c;
}


#pragma GCC optimize ("03")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define sz(x) (ll)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define nl "\n"

const int mod=100000007;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    //
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) {
		cin >> a[i];
	}
	int sum=0;
	if(is_sorted(a, a+n)) {
		cout << 0;
		return 0;
	}
	for(int i=0;i<n;i++) {
		if(i==n-2 && a[n-2]<=a[n-1]) {
			break;
		}
		int count=i+1;
		while(a[i]>a[count] && count<n) {
			a[count]++;
			count++;
		}
		if(a[i]>a[i+1] && i!=n-1) {
			i--;
		}
		sum++;
		if(i==n-2 && a[n-2]<=a[n-1]) {
			break;
		}
	}
	cout << sum;
}


#pragma GCC optimize ("03")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define sz(x) (ll)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define nl "\n"

const int mod=100000007;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    //
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) {
		cin >> a[i];
	}
	int sum=0;
	if(is_sorted(a, a+n)) {
		cout << 0;
		return 0;
	}
	for(int i=0;i<n-1;i++) {
		int diff=a[i]-a[i+1];
		if(a[i]>a[i+1]) {
			sum+=a[i]-a[i+1];
		}else {
			continue;
		}
	}
	cout << sum;
}


#pragma GCC optimize ("03")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define sz(x) (ll)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define nl "\n"

const int mod=100000007;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    //
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) {
		cin >> a[i];
	}
	ll sum=0;
	if(is_sorted(a, a+n)) {
		cout << 0;
		return 0;
	}
	for(int i=0;i<n-1;i++) {
		int diff=a[i]-a[i+1];
		if(a[i]>a[i+1]) {

		}else {
			continue;
		}
		int count=i;
		while(a[count]>a[count+1] && count<n-1) {
			a[count+1]+=diff;
			count++;
		}
		sum+=diff;
	}
	cout << sum;
}


#pragma GCC optimize ("03")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define sz(x) (ll)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define nl "\n"

const int mod=100000007;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    //
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) {
		cin >> a[i];
	}
	ll sum=0;
	if(is_sorted(a, a+n)) {
		cout << 0;
		return 0;
	}
	for(int i=0;i<n-1;i++) {
		int diff=a[i]-a[i+1];
		if(a[i]>a[i+1]) {

		}else {
			continue;
		}
		int count=i;
		if(n<=n-1) {
			while(a[count]>a[count+1] && count<n-1) {
				a[count+1]+=diff;
				count++;
			}
		}
		sum+=diff;
	}
	cout << sum;
}


#pragma GCC optimize ("03")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define sz(x) (ll)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define nl "\n"

const int mod=100000007;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
    //
	int n;
	cin >> n;
	int a[n];
	for(int i=0;i<n;i++) {
		cin >> a[i];
	}
	int sum=0;
	if(is_sorted(a, a+n)) {
		cout << 0;
		return 0;
	}
	for(int i=0;i<n-1;i++) {
		int diff=a[i]-a[i+1];
		if(a[i]>a[i+1]) {

		}else {
			continue;
		}
		int count=i;
		while(a[count]>a[count+1] && count<n-1) {
			a[count+1]+=diff;
			count++;
		}
		sum+=diff;
	}
	cout << sum;
}


#include<bits/stdc++.h>
using namespace std;
#define N 1009
int main(){

    int d;
    cin>>d;
    int cnt=0;
    int a1[d];
    for(int i=0;i<d;i++)
        cin>>a1[i];
    if(d==1)
    {
        cout<<0<<endl;
        return 0;
    }
        for(int i=0;i+1<d;i++)
        {
            if(a1[i]>a1[i+1])
            {
                cnt+=a1[i]-a1[i+1];
            }
        }
        cout<<cnt<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef   long long int  ll;
int main()
{
  // freopen("input.txt","r",stdin);
   ll  n,a,b,c,d;
   cin>>n;
   ll a1[n+1];
   for(ll i=1;i<=n;i++)
   {
       cin>>a1[i];
   }
   a=d=0;
   for(ll i=1;i+1<=n;i++)
   {
       if(a1[i]>a1[i+1])
       {
           b=a1[i]-a1[i+1];
          a=max(a,b);
         // cout<<a<<" --"<<endl;
          a1[i+1]=a1[i];
       }
       else
       {
           d=d+a;
          // cout<<d<<endl;
           a=0;
       }

   }
   cout<<d+a<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define N 1009
typedef long long int ll ;
int main(){

    ll d;
    cin>>d;
    ll cnt=0;
    ll a1[d];
    for(ll i=0;i<d;i++)
        cin>>a1[i];
    if(d==1)
    {
        cout<<0<<endl;
        return 0;
    }
        for(ll i=0;i+1<d;i++)
        {
            if(a1[i]>a1[i+1])
            {
                cnt+=a1[i]-a1[i+1];
            }
        }
        cout<<cnt<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef   long long int  ll;
int main()
{
   //freopen("input.txt","r",stdin);
   ll  n,a,b,c,d;
   cin>>n;
   ll a1[n+1];
   for(ll i=1;i<=n;i++)
   {
       cin>>a1[i];
   }
   a=d=0;
   for(ll i=1;i+1<=n;i++)
   {
       if(a1[i]>=a1[i+1])
       {
           b=a1[i]-a1[i+1];
          a=max(a,b);
          a1[i+1]=a1[i];
       }
       else
       {
           d=d+a;
           a=0;
       }

   }
   cout<<d+a<<endl;
}


//Yeh Toh Sahi Hoga
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
//#define int long long
#define pb push_back
#define lelo 1000003
#define pll pair<ll,ll>
#define V(n) vector <n>
#define F first
#define S second
#define nl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define clber cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define rep(i,iv,n) for(int (i)=(iv);(i)<(n);(i)++)
#define debug(x) cerr<<#x<<" = "<<x<< endl;
#define pi 3.1415926536

void prv(vector <ll> v)
{
   int n=v.size();
 // if(n>=0)
   for(int i=0;i<n;i++)
     cout<<v[i]<<" ";
   //  cout<<nl;
}

void swap(ll &a,ll &b)
{ int tm=a; a=b; b=tm; }

bool comp(const pair<int,int> &p1,const pair<int,int> &p2)
{
  if(p1.F>p2.F)
  return 1;
 /* else if(p1.F==p2.F)
  {
    if(p1.S>p2.S)
      return 1;
      else
      return 0;
     } */
     else
     return 0;

}

  const int N=2e5+5;
ll a[N];
void solve()
{
  ll n,sm=0;
  cin>>n;
  rep(i,0,n)
  cin>>a[i];

  rep(i,1,n-1)

     if(a[i]>a[i+1])
          sm+=a[i]-a[i+1];

    cout<<sm;

}

signed int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

    //freopen("input.txt", "r+", stdin);

    //freopen("output.txt", "w+", stdout);

  int t=1;
 //  cin>>t;
  while(t--)
  {
   solve();
   cout<<"\n";
  }

 return 0;
 }


//Yeh Toh Sahi Hoga
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
//#define int long long
#define pb push_back
#define lelo 1000003
#define pll pair<ll,ll>
#define V(n) vector <n>
#define F first
#define S second
#define nl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define clber cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define rep(i,iv,n) for(int (i)=(iv);(i)<(n);(i)++)
#define debug(x) cerr<<#x<<" = "<<x<< endl;
#define pi 3.1415926536

void prv(vector <ll> v)
{
   int n=v.size();
 // if(n>=0)
   for(int i=0;i<n;i++)
     cout<<v[i]<<" ";
   //  cout<<nl;
}

void swap(ll &a,ll &b)
{ int tm=a; a=b; b=tm; }

bool comp(const pair<int,int> &p1,const pair<int,int> &p2)
{
  if(p1.F>p2.F)
  return 1;
 /* else if(p1.F==p2.F)
  {
    if(p1.S>p2.S)
      return 1;
      else
      return 0;
     } */
     else
     return 0;

}

  const int N=2e5+5;
ll a[N];
void solve()
{
  ll n,sm=0;
  cin>>n;
  rep(i,0,n)
  cin>>a[i];

  rep(i,0,n-1)

     if(a[i]>a[i+1])
          sm+=a[i]-a[i+1];

    cout<<sm;

}

signed int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

    //freopen("input.txt", "r+", stdin);

    //freopen("output.txt", "w+", stdout);

  int t=1;
 //  cin>>t;
  while(t--)
  {
   solve();
   cout<<"\n";
  }

 return 0;
 }


//Yeh Toh Sahi Hoga
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
//#define int long long
#define pb push_back
#define lelo 1000003
#define pll pair<ll,ll>
#define V(n) vector <n>
#define F first
#define S second
#define nl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define clber cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define rep(i,iv,n) for(int (i)=(iv);(i)<(n);(i)++)
#define debug(x) cerr<<#x<<" = "<<x<< endl;
#define pi 3.1415926536

void prv(vector <ll> v)
{
   int n=v.size();
 // if(n>=0)
   for(int i=0;i<n;i++)
     cout<<v[i]<<" ";
   //  cout<<nl;
}

void swap(ll &a,ll &b)
{ int tm=a; a=b; b=tm; }

bool comp(const pair<int,int> &p1,const pair<int,int> &p2)
{
  if(p1.F>p2.F)
  return 1;
 /* else if(p1.F==p2.F)
  {
    if(p1.S>p2.S)
      return 1;
      else
      return 0;
     } */
     else
     return 0;

}

  const int N=3e5+5;
//ll a[N];
void solve()
{
  ll n,x;
  vector<ll> v;
  cin>>n;
   rep(i,0,n)
   { cin>>x;
     v.pb(x);
     }
     x=0;
     int t1=0,t2=n-1,id;
     ll mn,mx;
     while(t2>0)
{   mx=0;
  for(int i=t2;i>=t1;i--)
  {
     if(v[i]>mx)
     {
        mx=v[i];
        id=i;
      }

      }
      mn=mx;
      for(int i=id;i<=t2;i++)
         mn=min(mn,v[i]);

     x+=mx-mn;
     t2=id-1;

 }
   cout<<x;

}

signed int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

    //freopen("input.txt", "r+", stdin);

    //freopen("output.txt", "w+", stdout);

  int t=1;
 //  cin>>t;
  while(t--)
  {
   solve();
   cout<<"\n";
  }

 return 0;
 }


//Yeh Toh Sahi Hoga
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
//#define int long long
#define pb push_back
#define lelo 1000003
#define pll pair<ll,ll>
#define V(n) vector <n>
#define F first
#define S second
#define nl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define clber cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define rep(i,iv,n) for(int (i)=(iv);(i)<(n);(i)++)
#define debug(x) cerr<<#x<<" = "<<x<< endl;
#define pi 3.1415926536

void prv(vector <ll> v)
{
   int n=v.size();
 // if(n>=0)
   for(int i=0;i<n;i++)
     cout<<v[i]<<" ";
   //  cout<<nl;
}

void swap(ll &a,ll &b)
{ int tm=a; a=b; b=tm; }

bool comp(const pair<int,int> &p1,const pair<int,int> &p2)
{
  if(p1.F>p2.F)
  return 1;
 /* else if(p1.F==p2.F)
  {
    if(p1.S>p2.S)
      return 1;
      else
      return 0;
     } */
     else
     return 0;

}

  const int N=3e5+5;
//ll a[N];
void solve()
{
  ll n,x;
  vector<ll> v;
  cin>>n;
   rep(i,0,n)
   { cin>>x;
     v.pb(x);
     }
     x=0;
  //   prv(v);
     vector<ll>::iterator t1,t2,m;

     t1=v.begin(); t2=v.end();

     while(t2!=v.begin())
{
  m=max_element(t1,t2);
     x+=*m-*min_element(m,t2);
     t2=m;

 }
   cout<<x;

}

signed int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

    //freopen("input.txt", "r+", stdin);

    //freopen("output.txt", "w+", stdout);

  int t=1;
 //  cin>>t;
  while(t--)
  {
   solve();
   cout<<"\n";
  }

 return 0;
 }


//Yeh Toh Sahi Hoga
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
//#define int long long
#define pb push_back
#define lelo 1000003
#define pll pair<ll,ll>
#define V(n) vector <n>
#define F first
#define S second
#define nl "\n"
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define clber cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define rep(i,iv,n) for(int (i)=(iv);(i)<(n);(i)++)
#define debug(x) cerr<<#x<<" = "<<x<< endl;
#define pi 3.1415926536

void prv(vector <ll> v)
{
   int n=v.size();
 // if(n>=0)
   for(int i=0;i<n;i++)
     cout<<v[i]<<" ";
   //  cout<<nl;
}

void swap(ll &a,ll &b)
{ int tm=a; a=b; b=tm; }

bool comp(const pair<int,int> &p1,const pair<int,int> &p2)
{
  if(p1.F>p2.F)
  return 1;
 /* else if(p1.F==p2.F)
  {
    if(p1.S>p2.S)
      return 1;
      else
      return 0;
     } */
     else
     return 0;

}

  const int N=2e5+5;
ll a[N];
void solve()
{
  ll n,sm=0,mn,cm;
  cin>>n;
  rep(i,0,n)
  cin>>a[i];
   ll t=a[0];
   mn=cm=1e10+1;
  rep(i,1,n)
  {
     if(t<=a[i])
    {
      if(mn!=cm)
      {  sm+=t-mn;
        mn=cm;
       }
       t=a[i];
     }

     else
     {
       mn=min(mn,a[i]);

     }
    }
    if(mn!=cm)
    sm+=t-mn;
    cout<<sm;

}

signed int main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

    //freopen("input.txt", "r+", stdin);

    //freopen("output.txt", "w+", stdout);

  int t=1;
 //  cin>>t;
  while(t--)
  {
   solve();
   cout<<"\n";
  }

 return 0;
 }


#include<bits/stdc++.h>
//#include <boost/lexical_cast.hpp>
using namespace std;
#define ulli unsigned long long int

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    int *a = new int[n];
    cin>>a[0];
    long long int deficit = 0;
    long long int mov = 0;
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        /*if(defict==0)
        {
            if(a[i]<a[i-1])
            {
                deficit = a[i-1] - a[i];
                a[i] += deficit;
                mov += deficit;
                continue;
            }

        }*/
        if(a[i]<a[i-1])
        {
            long long int dif = a[i-1]-a[i];
            if(dif>deficit)
            {
                mov += dif - deficit;

            }
            deficit = dif;
            a[i]+= dif;
        }
        else
        {
            deficit = 0;
        }

    }
    cout<<mov<<endl;

   //cout<<"kill me"<<endl;
    return 0;

}


#include<bits/stdc++.h>
//#include <boost/lexical_cast.hpp>
using namespace std;
#define ulli unsigned long long int

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    int *a = new int[n];
    cin>>a[0];
    long long int deficit = 0;
    long long int mov = 0;
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        /*if(defict==0)
        {
            if(a[i]<a[i-1])
            {
                deficit = a[i-1] - a[i];
                a[i] += deficit;
                mov += deficit;
                continue;
            }

        }*/
        if(a[i]<=a[i-1])
        {
            long long int dif = a[i-1]-a[i];
            if(dif>deficit)
            {
                mov += dif - deficit;
                deficit = dif;

            }
            a[i]+= dif;
        }
        else
        {
            deficit = 0;
        }

    }
    cout<<mov<<endl;

   //cout<<"kill me"<<endl;
    return 0;

}


#include<bits/stdc++.h>
//#include <boost/lexical_cast.hpp>
using namespace std;
#define ulli unsigned long long int

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    int *a = new int[n];
    cin>>a[0];
    long long int deficit = 0;
    long long int mov = 0;
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        /*if(defict==0)
        {
            if(a[i]<a[i-1])
            {
                deficit = a[i-1] - a[i];
                a[i] += deficit;
                mov += deficit;
                continue;
            }

        }*/
        if(a[i]<a[i-1])
        {
            long long int dif = a[i-1]-a[i];
            if(dif>deficit)
            {
                mov += dif - deficit;
                deficit = dif;

            }
            a[i]+= dif;
        }
        else
        {
            deficit = 0;
        }

    }
    cout<<mov<<endl;

   //cout<<"kill me"<<endl;
    return 0;

}


#include<bits/stdc++.h>
//#include <boost/lexical_cast.hpp>
using namespace std;
#define ulli unsigned long long int

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n;
    cin>>n;
    int *a = new int[n];
    cin>>a[0];
    int left = a[0];
    int deficit = 0;
    int mov = 0;
    for(int i=1;i<n;i++)
    {
        cin>>a[i];
        /*if(defict==0)
        {
            if(a[i]<a[i-1])
            {
                deficit = a[i-1] - a[i];
                a[i] += deficit;
                mov += deficit;
                continue;
            }

        }*/
        if(a[i]<a[i-1])
        {
            int dif = a[i-1]-a[i];
            if(dif>deficit)
            {
                mov += dif - deficit;
                deficit = dif;

            }
            a[i]+= dif;
        }
        else
        {
            deficit = 0;
        }

    }
    cout<<mov<<endl;

   //cout<<"kill me"<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 300000000000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
int main()
{
    ll n;
    cin>>n;
    vll vec,ara;
    vll ::iterator it1,it,it2;
    for(ll i=0;i<n;i++)
    {
        ll num;
        cin>>num;
        vec.pb(num);
    }
    it=max_element(vec.begin(),vec.end());
    ll sum=0,g=distance(vec.begin(),it);
    it1=min_element(vec.begin()+g,vec.end());
    //cout<<*it1<<endl;
    ll k=*it,d=*it1;
    sum+=(k-d);
  // cout<<sum<<endl;
    while(distance(vec.begin(),it)>=0)
    {
        ll p=distance(vec.begin(),it);
        if(p>0)
        p--;
        it1=max_element(vec.begin(),vec.begin()+p);
        ll q=distance(vec.begin(),it1);
        it2=min_element(vec.begin()+q,vec.begin()+p+1);
        sum+=(*it1-*it2);
        it=it1;
        break;

    }
    cout<<sum<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 300000000000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
int main()
{
    ll n;
    cin>>n;
    vll vec,ara;
    vll ::iterator it1,it,it2;
    for(ll i=0;i<n;i++)
    {
        ll num;
        cin>>num;
        vec.pb(num);
    }
    ll hi=vec[0],sum=0;
    it=max_element(vec.begin(),vec.end());
    ll p=distance(vec.begin(),it);
    for(ll i=1;i<p;i++)
    {
       if(hi>vec[i])
       {
           ll lo=vec[i],j;
           for(j=i+1;j<p;j++)
           {
               if(lo<=vec[j])
               {
                   break;
               }
               else
               {
                   lo=vec[j];
               }
           }
           sum+=(hi-lo);
          // cout<<sum<<endl;
           if(j!=p)
           hi=max(hi,vec[j]);
           i=j-1;
       }
       else
        hi=vec[i];
    }
    sum+=(vec[p]-*(min_element(vec.begin()+p,vec.end())));
    cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 300000000000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
int main()
{
    ll n;
    cin>>n;
    vll vec,ara;
    vll ::iterator it1,it,it2;
    for(ll i=0;i<n;i++)
    {
        ll num;
        cin>>num;
        vec.pb(num);
    }
    ll hi=vec[0],sum=0,p=0,k=minf;
    for(ll i=0;i<n;i++)
    {
        if(k<=vec[i])
        {
            k=vec[i];
            p=i;
        }
    }
    for(ll i=1;i<p;i++)
    {
       if(hi>vec[i])
       {
           ll lo=vec[i],j;
           for(j=i+1;j<p;j++)
           {
               if(lo<=vec[j])
               {
                   break;
               }
               else
               {
                   lo=vec[j];
               }
           }
           sum+=(hi-lo);
          // cout<<sum<<endl;
           if(j!=p)
           hi=max(hi,vec[j]);
           i=j-1;
       }
       else
        hi=vec[i];
    }
    sum+=(vec[p]-*(min_element(vec.begin()+p,vec.end())));
    cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ull  unsigned long long
#define vll  vector <long long>
#define pll pair <long long,long long>
#define f first
#define s second
#define up upper_bound
#define lp lower_bound
#define pq priority_queue
#define inf 1e10
#define minf -1e10
#define pi 3.14159265
#define mod 300000000000000000
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);
int main()
{
    ll n;
    cin>>n;
    vll vec,ara;
    vll ::iterator it1,it,it2;
    for(ll i=0;i<n;i++)
    {
        ll num;
        cin>>num;
        vec.pb(num);
    }
    ll sum=0,mx=vec[0];
    for(ll i=0;i<n;i++)
    {
        if(mx>vec[i])
        {
            sum+=(mx-vec[i]);
        }
        mx=vec[i];
    }
    cout<<sum<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define PI 2 * acos(0)
#define ones(n) __builtin_popcount(n)
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX 1000006
#define debug(x) cerr << #x << " = " << x << '\n';
#define LOOP printf("LOOP!!")
#define READ freopen("input.txt", "r", stdin)
#define PRINT freopen("output.txt", "w", stdout);
#define print(a, b) printf("Case %d: %d\n", a, b)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) sort(all(v)), v.erase(unique(ALL(v)), v.end())
const ll MOD = 1e9 + 7;
using namespace std;

bool isPrime(ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 2)
        if (n % i == 0)
            return false;

    return true;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    ll ans = (a * b) / g;
    return ans;
}

///palindrome check!
bool palindrome(string s)
{
    for (ll i = 0; i < s.length() / 2; i++)
    {
        if (s[i] != s[s.length() - i - 1])
            return 0;
    }
    return 1;
}

/// divisors->>
void printDivisors(int n)
{
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (n / i == i)
                printf("%d ", i);

            else

                printf("%d %d ", i, n / i);
        }
    }
}
/// string to INT
int convert_number(string s)
{
    int n;
    stringstream(s) >> n;
    return n;

    // int v=atoi(ss.c_str());
    // atoi is better than this function in case of complexity
}

///prime storage>>
vector<ll> prime;
void primes(ll m)
{

    bool isprime[m];
    for (int i = 0; i <= m; i++)
        isprime[i] = true;

    for (int i = 3; i * i <= m; i++)
    {
        if (isprime[i])
        {

            for (int j = i * i; j <= m; j += i)
                isprime[j] = false;
        }
    }
    prime.push_back(2);
    for (int i = 3; i <= m; i += 2)
        if (isprime[i])
            prime.push_back(i);
}

int findsum(int n)
{
    int sum = 0;
    if (n != 0)
        return sum = n + findsum(n - 1);
    else
        return 0;
}

/************MAIN_FUNCTION***********/
int main(int argc, char *argv[])
{

    FAST;
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];

    ll l = INT_MAX, r = INT_MIN;
    int c=1;

    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] > v[i + 1])
        {
            if (v[i + 1] == l && v[i] == r)
                c++;
            l = min(l, v[i + 1]);
            r = max(r, v[i]);
        }
    }

    if (l == INT_MAX || r == INT_MIN)
    {
        cout << "0" << '\n';
    }

    else
        cout << (ll)c*(r - l) << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define PI 2 * acos(0)
#define ones(n) __builtin_popcount(n)
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX 1000006
#define debug(x) cerr << #x << " = " << x << '\n';
#define LOOP printf("LOOP!!")
#define READ freopen("input.txt", "r", stdin)
#define PRINT freopen("output.txt", "w", stdout);
#define print(a, b) printf("Case %d: %d\n", a, b)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) sort(all(v)), v.erase(unique(ALL(v)), v.end())
const ll MOD = 1e9 + 7;
using namespace std;

bool isPrime(ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 2)
        if (n % i == 0)
            return false;

    return true;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    ll ans = (a * b) / g;
    return ans;
}

///palindrome check!
bool palindrome(string s)
{
    for (ll i = 0; i < s.length() / 2; i++)
    {
        if (s[i] != s[s.length() - i - 1])
            return 0;
    }
    return 1;
}

/// divisors->>
void printDivisors(int n)
{
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (n / i == i)
                printf("%d ", i);

            else

                printf("%d %d ", i, n / i);
        }
    }
}
/// string to INT
int convert_number(string s)
{
    int n;
    stringstream(s) >> n;
    return n;

    // int v=atoi(ss.c_str());
    // atoi is better than this function in case of complexity
}

///prime storage>>
vector<ll> prime;
void primes(ll m)
{

    bool isprime[m];
    for (int i = 0; i <= m; i++)
        isprime[i] = true;

    for (int i = 3; i * i <= m; i++)
    {
        if (isprime[i])
        {

            for (int j = i * i; j <= m; j += i)
                isprime[j] = false;
        }
    }
    prime.push_back(2);
    for (int i = 3; i <= m; i += 2)
        if (isprime[i])
            prime.push_back(i);
}

int findsum(int n)
{
    int sum = 0;
    if (n != 0)
        return sum = n + findsum(n - 1);
    else
        return 0;
}

/************MAIN_FUNCTION***********/
int main(int argc, char *argv[])
{

    FAST;
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];

    ll l = INT_MAX, r = INT_MIN;

    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] > v[i + 1])
        {
            l = min(l, v[i + 1]);
            r = max(r, v[i]);
        }
    }

    if (l == INT_MAX || r == INT_MIN)
    {
        cout << "0" << '\n';
    }

    else
        cout << r - l << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define PI 2 * acos(0)
#define ones(n) __builtin_popcount(n)
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX 1000006
#define debug(x) cerr << #x << " = " << x << '\n';
#define LOOP printf("LOOP!!")
#define READ freopen("input.txt", "r", stdin)
#define PRINT freopen("output.txt", "w", stdout);
#define print(a, b) printf("Case %d: %d\n", a, b)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) sort(all(v)), v.erase(unique(ALL(v)), v.end())
const ll MOD = 1e9 + 7;
using namespace std;

bool isPrime(ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 2)
        if (n % i == 0)
            return false;

    return true;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    ll ans = (a * b) / g;
    return ans;
}

///palindrome check!
bool palindrome(string s)
{
    for (ll i = 0; i < s.length() / 2; i++)
    {
        if (s[i] != s[s.length() - i - 1])
            return 0;
    }
    return 1;
}

/// divisors->>
void printDivisors(int n)
{
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (n / i == i)
                printf("%d ", i);

            else

                printf("%d %d ", i, n / i);
        }
    }
}
/// string to INT
int convert_number(string s)
{
    int n;
    stringstream(s) >> n;
    return n;

    // int v=atoi(ss.c_str());
    // atoi is better than this function in case of complexity
}

///prime storage>>
vector<ll> prime;
void primes(ll m)
{

    bool isprime[m];
    for (int i = 0; i <= m; i++)
        isprime[i] = true;

    for (int i = 3; i * i <= m; i++)
    {
        if (isprime[i])
        {

            for (int j = i * i; j <= m; j += i)
                isprime[j] = false;
        }
    }
    prime.push_back(2);
    for (int i = 3; i <= m; i += 2)
        if (isprime[i])
            prime.push_back(i);
}

int findsum(int n)
{
    int sum = 0;
    if (n != 0)
        return sum = n + findsum(n - 1);
    else
        return 0;
}

/************MAIN_FUNCTION***********/
int main(int argc, char *argv[])
{

    FAST;
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];
    ll res=0;
    for (int i = 0; i < n - 1; i++)
        if (v[i] > v[i + 1])
            res += (v[i] - v[i+1]);

    cout << res << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define PI 2 * acos(0)
#define ones(n) __builtin_popcount(n)
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX 1000006
#define debug(x) cerr << #x << " = " << x << '\n';
#define LOOP printf("LOOP!!")
#define READ freopen("input.txt", "r", stdin)
#define PRINT freopen("output.txt", "w", stdout);
#define print(a, b) printf("Case %d: %d\n", a, b)
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) sort(all(v)), v.erase(unique(ALL(v)), v.end())
const ll MOD = 1e9 + 7;
using namespace std;

bool isPrime(ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;

    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (ll i = 5; i * i <= n; i = i + 2)
        if (n % i == 0)
            return false;

    return true;
}
ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    ll ans = (a * b) / g;
    return ans;
}

///palindrome check!
bool palindrome(string s)
{
    for (ll i = 0; i < s.length() / 2; i++)
    {
        if (s[i] != s[s.length() - i - 1])
            return 0;
    }
    return 1;
}

/// divisors->>
void printDivisors(int n)
{
    for (int i = 1; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            if (n / i == i)
                printf("%d ", i);

            else

                printf("%d %d ", i, n / i);
        }
    }
}
/// string to INT
int convert_number(string s)
{
    int n;
    stringstream(s) >> n;
    return n;

    // int v=atoi(ss.c_str());
    // atoi is better than this function in case of complexity
}

///prime storage>>
vector<ll> prime;
void primes(ll m)
{

    bool isprime[m];
    for (int i = 0; i <= m; i++)
        isprime[i] = true;

    for (int i = 3; i * i <= m; i++)
    {
        if (isprime[i])
        {

            for (int j = i * i; j <= m; j += i)
                isprime[j] = false;
        }
    }
    prime.push_back(2);
    for (int i = 3; i <= m; i += 2)
        if (isprime[i])
            prime.push_back(i);
}

int findsum(int n)
{
    int sum = 0;
    if (n != 0)
        return sum = n + findsum(n - 1);
    else
        return 0;
}

/************MAIN_FUNCTION***********/
int main(int argc, char *argv[])
{

    FAST;
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];

    int l = INT_MAX, r = INT_MIN;

    for (int i = 0; i < n - 1; i++)
    {
        if (v[i] > v[i + 1])
        {
            l = min(l, v[i + 1]);
            r = max(r, v[i]);
        }
    }

    if (l == INT_MAX || r == INT_MIN)
    {
        cout << "0" << '\n';
    }

    else
        cout << r - l << '\n';

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
	int n,i,j,t,c=-1;
	long long arr[2*n];
   cin>>n;
for(i=0;i<n;i++)
{
 cin>>arr[i];
}
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                t=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=t;
                c=c+1;
            }
        }
    }
cout<<c;
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
	int n,i,j,t,c=0;
	long long arr[2*n];
   cin>>n;
for(i=0;i<n;i++)
{
 cin>>arr[i];
}
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                t=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=t;
                c++;
            }
        }
    }
cout<<c-1;
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
	int n,i,j,t,c=0;
	long long arr[2*n];
   cin>>n;
for(i=0;i<n;i++)
{
 cin>>arr[i];
}
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                t=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=t;
                c++;
            }
        }
    }
cout<<c;
return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main()
{
	int n,i;
	long long a,b,sum=0;
   while(cin>>n)
   {
       for (i=1;i<=n;i++)
	{
		b=a;
		 scanf("%d",&a);
		if (i>1&&a<b)
		{
         sum+=b-a;
		}
	}
	cout<<sum<<endl;
   }

	return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#define MP make_pair
#define PB push_back
#define S size()
#define B begin()
#define E end()
#define ll long long
#define REP(i,a,n) for(ll i=a; i<n; ++i)
#define RED(i,n,a) for(ll i=n; i>=a; --i)
#define m9 1000000007
#define endl "\n"

using namespace std;

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll n;
    cin>>n;
    vector <ll> v(n);
    REP(i,0,n){
        cin>>v[i];
    }
    ll ans=0, strk=-1, bufferSum=0;
    REP(i,1,n){
        if(v[i]<v[i-1] && i>strk){
            ll x=i;
            while(v[x]<=v[i-1]){
                if(x+1<n){
                    x++;
                }
                else{
                    break;
                }
            }
            strk=x;
            ans+=v[i-1]-v[i];
            bufferSum=v[i-1]-v[i];
            v[i]=v[i-1];
        }
        else if(v[i]<v[i-1] && i<=strk){
            v[i]+=bufferSum;
            if(v[i]<v[i-1]){
                ans+=v[i-1]-v[i];
                bufferSum+=v[i-1]-v[i];
                v[i]=v[i-1];
            }
        }
        else if(v[i]>=v[i-1]){
            bufferSum=0;
        }
    }
    cout<<ans;

}


#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#define MP make_pair
#define PB push_back
#define S size()
#define B begin()
#define E end()
#define ll long long
#define REP(i,a,n) for(ll i=a; i<n; ++i)
#define RED(i,n,a) for(ll i=n; i>=a; --i)
#define m9 1000000007
#define endl "\n"

using namespace std;

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll n;
    cin>>n;
    vector <ll> v(n);
    REP(i,0,n){
        cin>>v[i];
    }
    ll ans=0, strk=-1, bufferSum=0;
    REP(i,1,n){
        if(v[i]<v[i-1] && i>strk){
            ll x=i;
            while(v[x]<=v[i-1]){
                x++;
                if(x==n){
                    break;
                }
            }
            strk=x-1;
            ans+=v[i-1]-v[i];
            bufferSum=v[i-1]-v[i];
            v[i]=v[i-1];
        }
        else if(v[i]<v[i-1] && i<=strk){
            v[i]+=bufferSum;
            if(v[i]<v[i-1]){
                ans+=v[i-1]-v[i];
                bufferSum+=v[i-1]-v[i];
                v[i]=v[i-1];
            }
        }
        else if(v[i]>=v[i-1]){
            bufferSum=0;
        }
    }
    cout<<ans;

}


#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#define MP make_pair
#define PB push_back
#define S size()
#define B begin()
#define E end()
#define ll long long
#define REP(i,a,n) for(ll i=a; i<n; ++i)
#define RED(i,n,a) for(ll i=n; i>=a; --i)
#define m9 1000000007
#define endl "\n"

using namespace std;

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll n;
    cin>>n;
    vector <ll> v(n);
    REP(i,0,n){
        cin>>v[i];
    }
    ll ans=0, strk=-1, bufferSum=0;
    REP(i,1,n){
        if(v[i]<v[i-1] && i>strk){
            ll x=i;
            while(v[x]<=v[x-1]){
                x++;
                if(x==n){
                    break;
                }
            }
            strk=x-1;
            ans+=v[i-1]-v[i];
            bufferSum=v[i-1]-v[i];
            v[i]=v[i-1];
        }
        else if(v[i]<v[i-1] && i<=strk){
            v[i]+=bufferSum;
            if(v[i]<v[i-1]){
                ans+=v[i-1]-v[i];
                bufferSum+=v[i-1]-v[i];
                v[i]=v[i-1];
            }
        }
        else if(v[i]>=v[i-1]){
            bufferSum=0;
        }
    }
    cout<<ans;

}


#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#define MP make_pair
#define PB push_back
#define S size()
#define B begin()
#define E end()
#define ll long long
#define REP(i,a,n) for(ll i=a; i<n; ++i)
#define RED(i,n,a) for(ll i=n; i>=a; --i)
#define m9 1000000007
#define endl "\n"

using namespace std;

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ll n;
    cin>>n;

    vector <ll> v(n);
    REP(i,0,n){
        cin>>v[i];
    }
    ll i=1, ans=0, str=-1, mn=1e9+1, mx=0, buffer=0, prevMax=-1;

    while(i<n){
        if(v[i]<v[i-1]){

            mx=v[i-1];
            while(v[i]<=v[i-1] && i<n){
                v[i-1]+=buffer;
                i++;
            }
            if(i==n){
                v[i-1]+=buffer;
                ans+=mx-v[i-1];
                cout<<ans;
                return 0;
            }
            else{

                mn=v[i-1];
                ans+=mx-v[i-1]-buffer;
            }
            while(i<n && v[i]>=v[i-1]){
                v[i-1]+=buffer;
                i++;
            }
            mx+=buffer;
            if(mx<=v[i-1]){
                buffer=0;
            }
            else{

                buffer=mx-v[i-1];
                v[i-1]=mx;

            }
        }
        else{
            i++;
        }
    }

    cout<<ans;

}


/*WELCOME TO AYUSH RANJAN CODE'S*/
/*EVERY PROBLEM HAS A SOLUTION SOMETIMES ELEGENT OTHERWISE COMPLEX ELEGENCE*/

#include <bits/stdc++.h>
using namespace std;
int chkpower2(int x){
    return (x&&!(x&x-1));
}

void solve(){
    int n;
    cin>>n;
    int a[n];
    for(int i1=0;i1<n;i1++){
        cin>>a[i1];
    }
   long long int i1=1,maxt=a[0],ans=0;
    while(i1<n){
        if(a[i1]>=a[i1-1]){
            maxt=a[i1];
            i1++;
            continue;
        }
        else{
            while(i1<n){
                if(a[i1]>a[i1-1]){
                    ans+=maxt-a[i1-1];
                    break;
                }
                if(a[i1]<=a[i1-1]){
                        i1++;
                    continue;
                }
            }
        }
    }
    cout<<ans;
}

int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);

    int t;
    t=1;//cin>>t;
    while(t--){
        solve();
        cout<<"\n";
    }
    return 0;
}


/*WELCOME TO AYUSH RANJAN CODE'S*/
/*EVERY PROBLEM HAS A SOLUTION SOMETIMES ELEGENT OTHERWISE COMPLEX ELEGENCE*/

#include <bits/stdc++.h>
using namespace std;
int chkpower2(int x){
    return (x&&!(x&x-1));
}

void solve(){
    int n;
    cin>>n;
    int a[n];
    for(int i1=0;i1<n;i1++){
        cin>>a[i1];
    }
   long long int i1=1,maxt=a[0],ans=0;
    while(i1<n){
        if(a[i1]>=maxt){
            maxt=a[i1];
            i1++;
            continue;
        }
        else{
            int c1=a[i1];
            while(a[i1]<maxt){
                if(i1>=n){
                    break;
                }
                if(a[i1]<c1){
                    c1=a[i1];
                }
                i1++;
                if(i1>=n){
                    break;
                }
            }
            ans+=maxt-c1;
        }
    }
    cout<<ans;
}

int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);

    int t;
    t=1;//cin>>t;
    while(t--){
        solve();
        cout<<"\n";
    }
    return 0;
}


/*WELCOME TO AYUSH RANJAN CODE'S*/
/*EVERY PROBLEM HAS A SOLUTION SOMETIMES ELEGENT OTHERWISE COMPLEX ELEGENCE*/

#include <bits/stdc++.h>
using namespace std;
int chkpower2(int x){
    return (x&&!(x&x-1));
}

void solve(){
    int n;
    cin>>n;
    int a[n];
    for(int i1=0;i1<n;i1++){
        cin>>a[i1];
    }
   long long int i1=1,maxt=a[0],ans=0;
    while(i1<n){
        if(a[i1]>=maxt){
            maxt=a[i1];
            i1++;
            continue;
        }
        else{
            vector<int> v1;
            while(a[i1]<maxt){
                    if(i1>=n){
                        break;
                    }
                   // cout<<a[i1]<<" ";
                v1.push_back(a[i1]);
                i1++;
            }
            sort(v1.begin(),v1.end());
            ans+=maxt-v1[v1.size()-1];
           // cout<<v1.size()<<" ";
            for(int i2=v1.size()-1;i2>0;i2--){
                  //  cout<<i2<<" "<<i2-1<<endl;
                    ans+=v1[i2]-v1[i2-1];
            }
        }
    }
    cout<<ans;
}

int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);

    int t;
    t=1;//cin>>t;
    while(t--){
        solve();
        cout<<"\n";
    }
    return 0;
}