using System;

class Program
{
    static void Main()
    {
        string N = Console.ReadLine();
        string A = Console.ReadLine();
        string[] a = (A.Split(' '));
        int[] an = new int[Convert.ToInt32(N)];
        int count = 0;

        for (int i = 0; i < Convert.ToInt32(N); i++)
        {
            an[i] = Convert.ToInt32(a[i]);
        }

        for (int i = 0; i < Convert.ToInt32(N)-1; i++)
        {
            if (an[i]>an[i+1])
            {
                count += an[i] - an[i + 1];
                an[i + 1] = an[i];
            }
        }

        Console.WriteLine(count);

    }
}