#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ll ans=0,n,arr[300005],i,t=0;
cin>>n;
for(i=0;i<n;i++)cin>>arr[i];
t=arr[0];
for(i=1;i<n;i++){
if(arr[i]<t){
ans+=t-arr[i];
}
else
t=arr[i];}
cout<<ans;}