using System;
using System.Linq;

namespace abc176.c
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = getIntParam();
            int[] paramArray = getIntParams();

            int adjust = 0;
            int level = paramArray[0];

            for (int c = 1; c < num; c++)
            {
                if (paramArray[c] < level)
                {
                    adjust += (level - paramArray[c]);
                }
                else if (paramArray[c] > level)
                {
                    level = paramArray[c];
                }
            }
            Console.WriteLine(adjust);
        }

        //↓↓↓ shared function ↓↓↓
        static int getIntParam()
        {
            return int.Parse(Console.ReadLine());
        }
        static int[] getIntParams()
        {
            return Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray();
        }
        static long getLongParam()
        {
            return long.Parse(Console.ReadLine());
        }
        static long[] getLongParams()
        {
            return Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
        }
        static string getStringParam()
        {
            return Console.ReadLine();
        }
        static string[] getStringParams()
        {
            return Console.ReadLine().Split(' ');
        }
    }
}
