using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Atcoder2
{
    class Program
    {

        public static void Main(string[] args)
        {
            int people = int.Parse(Console.ReadLine());
            string[] input = Console.ReadLine().Split(' ');
            int[] length = new int[people];
            for(int i = 0; i < people; i++)
            {
                length[i] = int.Parse(input[i]);
            }
            int ans = 0;

            for (int j = 1; j < people; j++)
            {
                int difference = length[j - 1] - length[j];
                if(difference > 0)
                {
                    ans = ans + difference;
                    length[j] = length[j] + difference;
                }
            }

            Console.WriteLine(ans);
        }

    }
}