#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    ll cnt=0;
    ll max_num = 0;
    for (int i=0; i<n; i++){
        cin >> a[i];
        int s = a[i];
        if (s < max_num){
            cnt += max_num - s;
        }
        max_num = max(max_num,s);


    }
    cout << cnt << endl;
    


    return 0;
}