program main                                            
  implicit none                                         
  
  integer, allocatable, dimension ( : ) :: A, tmp       
  integer :: n, i, j, M                                       
  
  read *, n                                             
  
  allocate ( A ( 1:n ) )                                
  
  do i = 1, n                                           
     read *, A ( i )                                    
  end do

  M = maxval ( A )
  
  do i = 1, n
     if ( A ( i ) == M ) then
        A ( i ) = 0
        if ( maxval ( A ) == M ) then
           do j = i, n
              print *, M
           end do
        else
           print *, maxval ( A )
           do j = i + 1, n
              print *, M
           end do
        end if
        exit
     else
        print *, maxval ( A )
     end if
  end do
  
end program main
