#include<bits/stdc++.h>
using namespace std;
#define hackcyborg shresth_walia
#define ll int
#define ld double
#define pb push_back
#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#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>
ll binpow(ll a,ll b,ll m=mod)
{
    ll res=1;
    while(b>0)
    {
        if(b&1)
        res=(a*res)%m;
        a=(a*a)%m;
        b/=2;
    }
    return res;
}
main()
{
    IO
    ll n;
    cin>>n;
    ll a[n];
    for(int x=0;x<n;x++)
        cin>>a[x];
    ll e=0;
    ll ans=0;
    for(int x=0;x<n;x++)
    {    if(e>a[x])
        ans+=abs(a[x]-e);
        e=max(e,a[x]);
    }
    cout<<ans;
}
