#inclue<iostream>
using namespace std;

int main(){
  int n;
  cin>>n;
  int a[n];
  int i;
  
  for(i=0;i<n;i++)
    cin>>a[i];
  int stools= 0;
  int diff;
 for(i=1;i<n;i++)
 {
   if(a[i-1]>a[i]){
     diff = (a[i-1]-a[i]); 
    stools+=diff;
     a[i] += diff;
   }
 }
  
  
  cout<<stools;
  
 return 0; 
}