#include<iostream>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int arr[n];
  int height = 0;
  cin>>arr[0];
  int max = arr[0];
  for(int i =1;i<n;i++)
  {
    cin>>arr[i];
    if(arr[i] >= max)
    {
      	max =arr[i];
    }
    else
    {
      height += max - arr[i];
    }
  }
  cout<<height<<endl
  
  
}