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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            int[] A = Console.ReadLine().Split(" ").Select(x => int.Parse(x)).ToArray();
            int ans = 0;
            int tmp = 0;
            for (int i = 1; i < N; i++)
            {
                if (A[i - 1] > A[i])
                {
                    tmp = A[i - 1] - A[i];
                    A[i] += tmp;
                    ans += tmp;
                }
            }
            Console.WriteLine(ans);
        }
    }
}
//int[] N = Console.ReadLine().Split(" ").Select(x => int.Parse(x)).ToArray();