#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int a;
  int i,j;
  int sum_a = 0;
  int sum_b = 0;
  int max_vec;
  int sum_dai_height;
  int vec_point;
  
 
  cin >> a ;
  
  vector <int>vec(a);
  for(i=0;i<a;i++){
    cin >> vec.at(i);
    }
  max_vec = vec.at(0);
  sum_dai_height = 0;
  
  for(i=0;i<a;i++){
  if(vec.at(i)>max_vec){
    max_vec = vec.at(i);
  }
    else {
      sum_dai_height = sum_dai_height + max_vec - vec.at(i);
    }
  }