include <iostream>
using namespace std;
long int height(vector<long int> &A,int N){
  int n = N;
  long 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;
}
long int main(){
  int n;
  cin>>n;
  vector<long int> A(n,-1) ;
  for(int i=0;i<n;i++){
    cin>>A[i];
  }
  long int ans = height(A,n);
  return ans;
}
  
