
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.WebSockets;

class Test
{

    // Driver code 
    public static void Main()
    {
        ///var t = int.Parse(Console.ReadLine());

        //while (t > 0)
        {
          //  t--;

            var ii = Console.ReadLine().Split(' ').Where(xx => !string.IsNullOrEmpty(xx)).Select(xx => int.Parse(xx)).ToArray();

            var n = ii[0];
           // var x = ii[1];
           // var tt = ii[2];

             var arr = Console.ReadLine().Split(' ').Where(x => !string.IsNullOrEmpty(x)).Select(xx => int.Parse(xx)).ToList();

            int ans = 0;

            for(int i=1;i<n;i++)
            {
                if(arr[i]<arr[i-1])
                {
                    ans = ans + arr[i - 1] - arr[i];
                    arr[i] = arr[i] + (arr[i - 1] - arr[i]);
                }
            }
            Console.Write(ans);
            Console.WriteLine();
        }
    }

}


