using System;
using System.Collections.Generic;

namespace Atc_0020
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());

            string str = Console.ReadLine();

            var nums = str.Split(' ');

            List<int> numList = new List<int>();

            for (int i = 0; i < nums.Length; i++)
            {
                numList.Add(int.Parse(nums[i]));
            }
            int ans = 0;

            for (int i = 0; i < numList.Count - 1; i++)
            {
                if (numList[i] > numList[i + 1])
                {
                    int d = numList[i] - numList[i + 1];

                    numList[i + 1] += d;

                    ans += d;
                 }
            }

            Console.WriteLine(ans);

        }
    }
}
