#include <bits/stdc++.h>

using namespace std;

long long n,k,a,b,c,q[200005],nr,rs;
string s,x;

int main() 
{
    cin>>n;
    for (int i=1; i<=n; i++){
        cin>>q[i];
        if (q[i]<q[i-1]) rs+=abs(q[i]-q[i-1]), q[i] = q[i-1];
    }
    cout<<rs;
}