using static System.Math;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace AtCoder
{
    class Program
    {
        private static long mod = 1000000007;
        static void Main(string[] args)
        {
            var n = int.Parse(ReadLine());
            var line = ReadLine().Split(' ').Select(value => int.Parse(value)).ToArray();

            var max = 0;
            var sum = 0;

            foreach(var a in line)
            {
                if(max > a)
                {
                    sum += max - a;
                }
                else
                {
                    max = a;
                }
            }

            WriteLine(sum);
        }
    }
}