#include <bits/stdc++.h>
#include <vector>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int> tall(N);
    int max=0
    long total_step=0;
    for (int i=0; i<N; i++) {
        cin >> tall[i];
        if (max > tall[i]) {
            total_step += max - tall[i];
        }
        if (tall[i] > max) {
            max = tall[i];
        }
    }

    cout << total_step;
}