using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var A = Console.ReadLine().Split().Select(int.Parse).ToArray();

            var height = A[0];
            var ans = 0;

            foreach (var i in A)
            {
                ans += Math.Max(height - i, 0);
                height = Math.Max(height, i);
            }

            Console.WriteLine(ans);
        }
    }
}
