#include <iostream>

using namespace std;

int main() {

    int n;
    cin >> n;
    int m = 0;
    long tool = 0;
    while (n--) {
        int val;
        cin >> val;
        m = max(m, val);
        if (m > val) tool += m - val;
    }
    cout<<tool;
}