program main
  implicit none
  integer(8) i,N,min,fumi
  integer(8),allocatable :: A(:)
  read(*, *)N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  fumi = 0
  min = 0

  do i = 1,N
    if (min > A(i)) then
      fumi = fumi + (min - A(i))
    else
      min = A(i)
    end if
  end do
  
  write(*, *) fumi
end program main
