/*
Jackson Simoneau
SI@UCF 2026 - Intro to Competitive Programming

Solution to Increasing Array: https://cses.fi/problemset/task/1094
*/

#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    int n; cin >> n;
    int prev = 0;
    ll ans = 0;
    
    for(int i = 0; i < n; i++) {
        int x; cin >> x;
        //Increase this value to the maximum previous value
        if(x < prev) {
            ans += prev-x;
        }
        //Update the previous value to the highest so far
        prev = max(prev, x);
    }

    //Display the answer
    cout << ans << endl;
}