using System;
using System.Linq;
namespace A
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var As = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();

            var ans = 0;
            var tmp = As[0];
            for (int i = 1; i < As.Count; i++)
            {
                if(tmp < As[i])
                {
                    tmp = As[i];
                }
                else
                {
                    int pls = tmp  - As[i];
                    ans += pls;
                }
            }
            System.Console.WriteLine(ans);
        }
    }
}
