#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
    if(!b)return a;
    else return gcd(b,a%b);
}
/*int n,q,ara[200001],st[800004],a,b,mx=2000000000;
void build(int si,int ss,int se)
{
    if(ss==se){
        st[si]=ara[ss];
        return;
    }
    int mid=(ss+se)/2;
    build(2*si,ss,mid),build(2*si+1,mid+1,se);
    st[si]=min(st[2*si],st[2*si+1]);
}
int query(int si,int ss,int se,int qs,int qe)
{
    if(qe<ss || se<qs)return mx;
    if(ss>=qs && qe>=se)return st[si];
    int mid=(ss+se)/2;
    return min(query(2*si,ss,mid,qs,qe),query(2*si+1,mid+1,se,qs,qe));
}
int main()
{
    //freopen ("input.txt","r",stdin);
    //freopen ("output1.txt","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>ara[i];
    build(1,1,n);
    while(q--){
        cin>>a>>b;
        cout<<query(1,1,n,a,b)<<"\n";
    }
}*/
int main()
{
    int n;
    cin>>n;
    int ara[n];
    long long int ans=0;
    cin>>ara[0];
    for(int i=1;i<n;i++){
        cin>>ara[i];
        if(ara[i]<ara[i-1])ans+=ara[i-1]-ara[i],ara[i]=ara[i-1],;
    }
    cout<<ans;
}
