using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace AtCoder
{
    class ABC168C
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int[] a = new int[n];
            string[] str = Console.ReadLine().Split(' ');
            for(int i = 0; i < n; i++)
            {
                a[i] = int.Parse(str[i]);
            }
            int count = 0;
            for(int i = 1; i < n; i++)
            {
                if(a[i]<a[i-1])
                {
                    count += a[i-1]-a[i];
                    a[i] = a[i-1];
                }
            }
            Console.WriteLine(count);
        }
    }
}