#include<bits/stdc++.h>
#include<ext/rope>
#define ll long long
#define ld long double
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
using namespace std;
using namespace __gnu_cxx;
const int N = 1e6 + 5;
const int mod = 998244353;
const ld pi = acos(-1.0);
int t,n,m,l,r,x,Case;
int a[N],ans;
void run(){
    cin>>n>>a[0];
    for(int i=1;i<n;i++){
        cin>>a[i];
        if(a[i]<a[i-1]){
            ans+=a[i-1]-a[i];
            a[i]=a[i-1];
        }
    }
    cout<<ans<<'\n';
}
int main(){
#ifdef haosao
freopen("main.h", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
//    while()
        run();
    return 0;
}
