using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Numerics;

namespace abc176
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
            if (File.Exists("input.txt"))
            {
                Console.SetIn(new StreamReader("input.txt"));
            }
            solve_abc176C();
            Console.Out.Flush();
        }

        public static void solve_abc176C()
        {
            long n = Convert.ToInt32(Console.ReadLine());
            long[] a = Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
            long cnt = 0;
            long max = a[0];
            for (int i = 1; i < n; i++)
            {
                if (a[i] < a[i - 1])
                {
                    cnt += max - a[i];
                }
                max = Math.Max(max, a[i]);
            }
            Console.WriteLine(cnt);
        }

        public static void solve_abc176B()
        {
            string s = Console.ReadLine();
            int sum = 0;
            for (int i = 0; i < s.Length; i++)
            {
                sum += Convert.ToInt32(s[i].ToString());
            }
            Console.WriteLine(sum % 9 == 0 ? "Yes" : "No");
        }

        public static void solve_abc176A()
        {
            int[] a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            int w = (a[0] + a[1] - 1) / a[1];
            Console.WriteLine(w * a[2]);
        }
    }
}