program road_to_millionaire
  implicit none
  integer :: n, a(80) = 0, b(80) = 0, i, k = 1, m = 2
  integer(8) :: x = 1000
  logical :: f1, f2
  read(*,*) n
  read(*,*) a(1:n)
  do while (k <= n - 1 .and. a(k) >= a(k + 1))
    k = k + 1
  end do
  if (k == n) then
    write(*,'(i0)') 1000
    stop
  end if
  b(1) = a(k)
  b(2) = a(k + 1)
  do i = k + 2, n
    f1 = b(m - 1) < b(m) .and. b(m) <= a(i)
    f2 = b(m - 1) > b(m) .and. b(m) >= a(i)
    if (.not.(f1 .or. f2)) m = m + 1
    b(m) = a(i)
  end do
  do i = 1, m, 2
    x = x / b(i) * b(i + 1) + mod(x, int(b(i), 8))
  end do
  write(*,'(i0)') x
end program road_to_millionaire