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