using System;

namespace Step
{
    class Program
    {
        static void Main(string[] args)
        {
            int N;
            int[] A = new int[100000000];

            int MaxTall;
            int Res =0;

            string inD0 = Console.ReadLine();
            N = int.Parse(inD0);

            string inD1 = Console.ReadLine();
            string[] cngD1 = inD1.Split(' ');

            for (int i = 0; i <= N - 1; i++)
            {
                A[i] = int.Parse(cngD1[i]);
            }

            MaxTall = A[0];

            for (int i =1; i < N ; i++)
            {
                if (MaxTall > A[i])
                {
                    Res += MaxTall - A[i];
                
                }
                else MaxTall = A[i];

            }
            Console.WriteLine(Res);
        }
    }
}
