program step
  implicit none
  integer :: n, i
  integer(8) :: a(200000) = 0, x = 0
  read(*,*) n
  read(*,*) a(1:n)
  do i = 2, n
    if (a(i) >= a(i - 1)) cycle
    x = x + a(i - 1) - a(i)
    a(i) = a(i - 1)
  end do
  write(*,'(i0)') x
end program step