#include<iostream>
using namespace std;
int main(){
int a;
  cin>>a;
  long int b[a],sum=0;
             for(int i=0;i<a;i++)cin>>b[i];
  for(int i=1;i<a;i++){
  if(b[i]<b[i-1])
  {sum+=b[i-1]-b[i];
  b[i]=b[i-1];}
  
  }
  cout<<sum;



return 0;}