#include <iostream>
using namespace std;
int height(int A[N],int N){
  int n = N;
  int sum =0;
  for(int i=0;i<n;i++){
    if(A[i+1]<=A[i]){
      sum+= (A[i]-A[i+1]);
      A[i+1] = A[i];
    }
  }
  return sum;
}
int main(){
  int n;
  cin>>n;
  int A[n] ;
  for(int i=0;i<n;i++){
    cin>>A[i];
  }
  int ans = height(A,n);
  cout<<ans;
  return 0;
}
  
