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 s;
            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++)
            {
                s = ii + 1;
                if (A[ii] > A[s])
                {
                    A[s] = A[ii];
                }
            }
            for (int k = 0;k < N;k++){
                d += A[k] - B[k];
            }
            Console.WriteLine(d);
        }
    }
}