using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;
using static AtCoderTemplate.MyLibrary;

namespace AtCoderTemplate
{
    class Program
    {
        static void Main (string[] args)
        {
            string[] str;
            str = Console.ReadLine ().Split (' ');

            int N = int.Parse (str[0]);
            int[] A = new int[N];

            str = Console.ReadLine ().Split (' ');
            for (int i = 0; i < N; i++)
            {
                A[i] = int.Parse (str[i]);
            }

            int ans = 0;
            for (int i = 1; i < N; i++)
            {
                if (A[i - 1] > A[i])
                {
                    ans += A[i - 1] - A[i];
                    A[i] = A[i - 1];
                }
            }

            Console.WriteLine (ans);

        }
    }

    class MyLibrary
    {
        public static int GCD (int a, int b)
        {
            if (a % b == 0) return b;
            return GCD (b, a % b);
        }
    }
}