using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        string[] input = Console.ReadLine().Split(' ');
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
        {
            a[i] = int.Parse(input[i]);
        }

        int r = 0;
        int h = 0;
        for (int i = 0; i < n; i++)
        {
            if (h > a[i])
            {
                r += h - a[i];
            }
            else
            {
                h = a[i];
            }
        }
        Console.WriteLine(r);
    }
}