using System;
using System.Linq;
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];

        if(N == 1)
        {
            Console.WriteLine(0);
        }
        else
        {
            for(int i=0; i<N; i++) {
                A[i] = int.Parse(input[i]);
            }

            int sum = 0;

            for(int i=1; i<N; i++) {
                if(A[i] < A[i-1]) {
                    int stage = A[i-1] - A[i];
                    sum += stage;
                    A[i] = A[i-1];
                }
            }

            Console.WriteLine(sum);
        }
    }
}