using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;

namespace ABC176
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            string[] input = Console.ReadLine().Split(" ");
            int[] A = new int[input.Length];
            for(var i = 0; i < input.Length; i++)
            {
                A[i] = int.Parse(input[i]);
            }

            int cnt = 0;

            for(var i = 1; i < N; i++)
            {
                if (A[i] < A[i - 1])
                {
                    int dif =  A[i - 1] - A[i];
                    cnt += dif;
                    A[i] += dif;
                }
            }
            Console.WriteLine(cnt);
            
        }
    }
}
