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

int main() {
    int N;
    cin >> N;
    int A[N];
    for (int i=0; i<N; i++){
        cin >> A[i];
    }
    int x=0;
    for (int i=1; i<=N; i++){
        if (A[i-1]<=A[i]) continue;
        else {
            x+=(A[i-1]-A[i]);
        }
    cout << x << end;
    return 0;
}