using System;
using System.Linq;

namespace AtCoder.Contests.Abc176
{
    public class Step
    {
        public static void Main()
        {
            Console.ReadLine();
            string input2 = Console.ReadLine();

            if (String.IsNullOrEmpty(input2)) { return; }

            var items = input2.Split(' ')
                .Select(x => Int32.Parse(x));
            int p = 0;
            int sum = 0;

            foreach (var item in items)
            {
                if (item < p)
                {
                    sum += p - item;
                }
                else
                {
                    p = item;
                }
            }

            Console.WriteLine(sum);
        }
    }
}
