program answer
  implicit none
  integer(8) :: i, N, ans, max
  integer(8), allocatable :: A(:)
  read(*,*) N
  allocate(A(n))
  read(*,*) A

  ans=0
  max=A(1)
  do i = 2, N
     if(A(i)<=max) then
        ans=ans+max-A(i)
     else
        max=A(i)
     end if
  end do

  write(*,*) ans
  deallocate(A)
  stop
 end program answer