using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using Console = System.Console;

namespace Atcoder
{

    class Program
    {
        private const long COMDIV = 1000000007;

        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            int sum = 0;
            var As = Console.ReadLine().Split().Select(int.Parse).ToArray();
            for (int i = 1; i < N; i++)
            {
                var pre = As[i - 1];
                var current = As[i];
                if (pre > current)
                {
                    var dif = pre - current;
                    sum += dif;
                    As[i] += dif;
                }
            }

            Console.WriteLine(sum);
        }
    }
}