#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n; cin >> n;
    int ar[n+5] = {};
    for(int i = 1; i <= n; i++)
    {
        cin >> ar[i];
    }
    int sum = 0;
    for(int i = 2; i <= n; i++)
    {
        if(ar[i] < ar[i-1])
        {
            sum += abs(ar[i-1]-ar[i]);
            ar[i] += abs(ar[i]-ar[i-1]);
        }
    }
    cout << sum << endl;
    return 0;
}
