    #include <iostream>
    #include <bits>
    using namespace std;
     
    int main()
    {
      int N;
      cin>>N;
      int sum=0;
      int arr[N];
      for (int i=0;i<N;i++)
      {
        int n;
        cin>>arr[i];
      }
      for (int i=0;i<N;i++)
      {
        j = i+1;
        while j<N
        {
         	if (arr[i]>arr[j])
            {
              sum+=arr[i]-arr[j];
              j++;
            }      
          	
          
          	else
            {
              break;
            }
        }
        
        if (arr[i]==*max_element(0,N-1))
        {
          break;
        }
      } 
      
      cout<<sum;
      
     return 0; 
    }