#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0;i<n;i++)
       cin>>arr[i];
    ll ans=0;
    for(ll i=0;i<n-1;i++)
    {
        if(arr[i]<=arr[i+1])
            ans+=abs(arr[i]-arr[i+1]);
    }
    cout<<ans<<"\n";
    return 0;