program main
  implicit none
  integer :: n, i, largest, second
  integer, allocatable :: a(:)
  read (*, *) n
  allocate (a(n))
  read (*, *) (a(i), i = 1, n)
  largest = 0
  second = 0
  do i = 1, n
    if (a(i) > second) then
      second = a(i)
      if (second > largest) call swap(second, largest)
    end if
  end do

  do i = 1, n
    if (a(i) == largest) then
      write (*, "(i0)") second
    else
      write (*, "(i0)") largest
    end if
  end do
  contains
  subroutine swap(a, b)
    integer, intent(inout) :: a, b
    integer :: c
    c = a
    a = b
    b = c
  end subroutine swap
end program main
