#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
    ll n;
    cin >> n;
    ll ar[n];
    for(ll  i = 0; i < n; i++)
    {
        cin>>ar[i];

    }
    ll cnt=0;
    for(lli=1; i< n ; i++){
        if(ar[i-1] >= ar[i]){
            cnt+=  abs(ar[i-1]- ar[i]);
             ar[i]=ar[i-1];
        }
    }
    cout<<cnt;
}

int main(){
    int t=1; 
    // cin>>t;
    while(t--){
   
    solve();
    }

}
