/*
    Author:    DreamCat4er
    Created:   22.08.2020 18:20:37
*/
#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n;
    cin >> n;
    vector <long long> a(n);
    for (long long &i : a)
        cin >> i;

    long long mx = a[0], ans = 0;
    for (int i = 1; i < n; ++i) {
        ans += max(0, mx - a[i]);
        mx = max(mx, a[i]);
    }

    cout << ans << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
                
    #ifdef _LOCAL
        int t;
        cin >> t;
        for (int _t = 0; _t < t; ++_t) {
            cout << "Case " << _t << " :\n";
            solve();
            cout << "\n";
        }
    #else
        solve();
    #endif // _LOCAL

    return 0;
}