        #include <iostream>
        #include <algorithm>
        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(arr,arr+N-1))
            {
              break;
            }
          } 
          
          cout<<sum;
          
         return 0; 
        }