#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

int n, a[200000];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i];
    long long ans = 0;
    for(int i = n - 2; i >= 0; i++){
        long long up = max(0, a[i + 1] - a[i]);
        a[i] += up, ans += up;
    }
    cout << ans << '\n';
    return 0;
}
