using System;

class Program
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        int[] a = InputIntArray();
        int count = 0;

        for(int i = 1; i < a.Length; i++)
        {
            if(a[i-1] > a[i])
            {
                int c =  a[i - 1] - a[i];
                a[i] += c;
                count += c;
            }

        }

        Console.WriteLine(count);

    }


    public static int[] InputIntArray()
    {
        string[] strs = Console.ReadLine().Split(' ');
        int[] num = new int[strs.Length];
        for (int i = 0; i < num.Length; i++)
        {
            num[i] = int.Parse(strs[i]);
        }

        return num;

    }
}


