using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace atcoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var buffer = Console.ReadLine().Split();
            var N = int.Parse(buffer[0]);

            var ans = 0;
            var before = -1;
            foreach (var v in Console.ReadLine().Split())
            {
                if (before == -1)
                {
                    before = int.Parse(v);
                    continue;
                }

                var tmp = int.Parse(v);
                var add = 0;
                if (before > tmp)
                    ans += add = before - tmp;

                before = tmp + add;
            }

            Console.WriteLine(ans);
        }
    }
}
