#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(void){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a.at(i);

    ll ans = 0;
    ll max_h = 0;

    rep(i, n){
        if(i==0){
            max_h = a.at(i);
            continue;
        }
        if(max_h > a.at(i)) ans += (max_h - a.at(i));
        max_h = max(max_h, a.at(i));
    }

    cout << ans << endl;

    return 0;
}