program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer,allocatable :: A2(:)
  integer :: i
  integer :: mloc_tmp(1)
  integer :: mval, mloc
  read(*,*) N
  allocate(A(N))
  allocate(A2(N-1))
  do i = 1,N
     read(*,*) A(i)
  end do
  mloc_tmp = maxloc(A)
  mval = maxval(A)
  mloc = mloc_tmp(1)
  !
  do i = 1,mloc-1
     A2(i) = A(i)
  end do
  do i = mloc+1,N
     A2(i-1) = A(i)
  end do
  !
  do i = 1,N
     if ( i == mloc ) then
        write(*,'(i0)') maxval(A2)
     else
        write(*,'(i0)') mval
     end if
  end do

  deallocate(A2,A)
  stop
end program main
