program exception_handling
    implicit none
    integer(4):: n, i,m1(2), m2(2), b

    m1(:)=0;m2(:)=0
    read*, n

    do i=1,n
        read*, b
        if (b > m1(1)) then
            m2(:) = m1(:)
            m1(1) = b
            m1(2) = i
        else if(b > m2(1)) then
            m2(1) = b
            m2(2) = i
        end if
    end do

    do i=1, n
        if (i == m1(2))then
            print*, m2(1)
        else
            print*, m1(1)
        end if
    end do

end program exception_handling