using System;

namespace AtBC176
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            string[] str = Console.ReadLine().Split();
            int d = 0;
           
            int[] A = new int[N];
            int[] B = new int[N];

            for (int i = 0;i < N; i++)
            {
                A[i] = int.Parse(str[i]);
                B[i] = int.Parse(str[i]);
        
            }
            for (int ii = 0;ii < N - 1; ii++)
            {
                
                if (A[ii] > A[ii + 1])
                {
                    d += A[ii] 
                   d -= A[ii + 1];
                    A[ii + 1] = A[ii];                   
                }
              else {}
            }
           
            Console.WriteLine(d);
        }
    }
}
