using System;
using System.Linq;

class Solution
{
    static int almostSorted(int[] arr)
    {
        var minHeigth = 0;
        var prevH = arr[0];
        for (var i = 1; i < arr.Length; i++)
        {
            if (arr[i] < prevH)
            {
                minHeigth += prevH - arr[i];
            }
            else 
            {
                prevH = arr[i];
            }
        }
        return minHeigth;
    }



    static void Main(string[] args)
    {
        var n = Console.ReadLine();
        var arr = Console.ReadLine().Split(' ').Select(x=> int.Parse(x)).ToArray();
        var result = almostSorted(arr);
        //var a = almostSorted(new[] { 10, 3 });
        Console.WriteLine(result);
    }
}