Imports System

Module Program
    Sub Main(args As String())

        Dim N As Integer = Integer.Parse(Console.ReadLine())
        Dim A() As Integer = Console.ReadLine().Split(" ").Select(Function(x) Integer.Parse(x)).ToArray()

        Dim ans As Long = 0

        If N <> 0 Then

            Dim bef As Integer = A(0)
            For i = 1 To N - 1
                If bef > A(i) Then
                    ans += bef - A(i)
                Else
                    bef = A(i)
                End If
            Next

        End If

        Console.WriteLine(ans)

    End Sub
End Module
