using System;
using static System.Console;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Atcoder_Answer
{
    class Program
    {
        const Int32 mod = 1000000007;

        static void Main(string[] args)
        {
            int N = int.Parse(ReadLine());
            string[] Reader = ReadLine().Split(' ');
            List<int> As = new List<int>();

            foreach (string i in Reader) 
            {
                As.Add(int.Parse(i));
            }

            int total_diffrence = 0;

            for (int i = 1; i <= N - 1; i++) 
            {
                
                int NewDiffrence = Math.Max(As[i] - As[i - 1], 0);
                total_diffrence += NewDiffrence;
                As[i] += NewDiffrence;
            }

            WriteLine(total_diffrence);

            return;
        }
    }
}
