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

public class AC
{


    public static int[] ReadArray()
    {
        return Array.ConvertAll(Console.ReadLine().Split(' '), m => Convert.ToInt32(m));
    }

    public static int ReadInt()
    {
        return Convert.ToInt32(Console.ReadLine());
    }

    public static string ReadStr()
    {
        return Console.ReadLine();
    }

    public static long ReadLong()
    {
        return Convert.ToInt64(Console.ReadLine());
    }

    public static long[] ReadLongArray()
    {
        return Array.ConvertAll(Console.ReadLine().Split(' '), m => Convert.ToInt64(m));

    }


    public static void Main()
    {
        int n = ReadInt();
        int[] A = ReadArray();

        int ans = 0;
        int max = 0;

        for(int i = 0; i < A.Length; i++)
        {
            if (A[i] < max)
                ans += max - A[i];

            max = Math.Max(max, A[i]);
        }

        Console.WriteLine(ans);



    }

}
