#include <iostream>
using namespace std;
int main() {
int N;
cin>>N;
int arr[N];
for(int i=0;i<N;i++)
{
cin>>arr[i];
}
int sum=0;
for(int i=0;i<N-1;i++)
{
if(arr[i] > arr[i+1])
{
sum+=arr[i]-arr[i+1];
arr[i+1]=arr[i];
}
}
cout<<sum<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CgkKCWludCBOOwoJY2luPj5OOwoJCglpbnQgYXJyW05dOwoJZm9yKGludCBpPTA7aTxOO2krKykKCXsKCQljaW4+PmFycltpXTsKCX0KCQoJaW50IHN1bT0wOwoJZm9yKGludCBpPTA7aTxOLTE7aSsrKQoJewoJCWlmKGFycltpXSA+IGFycltpKzFdKQoJCXsKCQkJc3VtKz1hcnJbaV0tYXJyW2krMV07CgkJCWFycltpKzFdPWFycltpXTsKCQl9Cgl9CgkKCWNvdXQ8PHN1bTw8ZW5kbDsKCQoJcmV0dXJuIDA7Cn0=