#include <iostream>
#include <string>
#include <map>
#include <set>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#define ll long long
//for (auto &x : a) { cin >> x; }
using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

    ll n;
    cin >> n;
    ll a[10e9];
    for(ll i=0; i<n; i++){
        cin >> a[i];
    }
    ll wm = 0;
    ll ans = 0;
    for(ll i=0; i<n; i++){
        if(a[i] < wm){
            ans += wm - a[i];
            a[i] = wm;
        }
        wm = max(wm, a[i]);
    }
    cout << ans;
}