import java.util.Scanner

fun main(args: Array<String>){
    val sc = Scanner(System.`in`)
    val N = sc.nextInt()
    var nowMax = 0
    var secondMax = 0
    var maxCounter = 0
    var maxPosition_ifUnique = 0
    for(i in 0 until N) {
        val tmp = sc.nextInt()
        if (tmp > nowMax) {
            secondMax = nowMax
            nowMax = tmp
            maxCounter = 1
            maxPosition_ifUnique = i
        }else if(tmp == nowMax) {
            maxCounter++
            maxPosition_ifUnique = 0
        }else if(tmp > secondMax){
            secondMax = tmp
        }
    }
    if(maxCounter == 1){
        for(i in 0 until maxPosition_ifUnique){
            println(nowMax)
        }
        println(secondMax)
        for(i in maxPosition_ifUnique + 1 until N){
            println(nowMax)
        }
    }else{
        for(i in 0 until N){
            println(nowMax)
        }
    }
}