using System;

namespace AtCoderSnippet
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var A = Console.ReadLine().Split(" ");
            var x = 0;
            
            for (int i = 0; i < N-1; i++)
            {
                var ax = int.Parse(A[i]);
                var ay = int.Parse(A[i+1]);
                if (ax > ay)
                {
                    var z = ax - ay;
                    A[i + 1] = (ay +z).ToString();
                    x += z;
                }
            }
            Console.WriteLine(x);
        }
    }
}
