using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder_20190707_1
{    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());

            int[] A = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();

            int max = A[0];
            int answer = 0;

            for(int i = 1; i < A.Length; i++)
            {
                if(A[i] > max)
                {
                    max = A[i];
                }
                else
                {
                    answer += max - A[i];
                }
            }

            Console.WriteLine(answer);
        }
    }
}