#include <iostream>

using namespace std;

int main()
{
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio(0);

    int N;
    cin >> N;
    int ar[200002];
    for (int i = 0; i < N; ++i){
        cin >> ar[i];
    }
    long long res = 0;
    for (int k = 0; k < N; ++k){
        if (ar[k] <= ar[k+1]) continue;
        else{
            res += ar[k] - ar[k+1];
        }
    }
    cout << res;
    return 0;
}
