using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;
using static System.Array;

class Program
{
	public static void Main()
	{
		var n = int.Parse(ReadLine());
		var a = ReadLine().Split().Select(int.Parse).ToArray();
		var sum = 0;
		for(int i = 1; i < n; i++)
		{
			if(a[i] < a[i - 1])
			{
				sum += a[i - 1] - a[i];
				a[i] = a[i - 1];
			}
		}
		WriteLine(sum);
	}
}
