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

namespace ABC086A
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var A = Console.ReadLine().Split(' ');
            var step = 0;

            for (int i = 1; i < N; i++)
            {
                if(int.Parse(A[i]) < int.Parse(A[i - 1])){
                    var diff = int.Parse(A[i - 1]) - int.Parse(A[i]);
                    step += diff;
                    A[i] = (int.Parse(A[i]) + diff).ToString();
                }
            }

            System.Console.WriteLine(step);
        }
    }
}
