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

int main(){
    ll t,n,k,l,r;
    ll res =0 ,pre = 0;
    cin>>n;
    for(ll i=0;i<n;i++){
        ll x; cin>>x;
        res +=max(0ll,pre-x);
        pre = max(pre,x);
    }
    cout<<res;
}
