N = gets.to_i
Pair = Struct.new(:a, :i)

top2 = [Pair.new(0, 0), Pair.new(0, 0)]
N.times{|_| i = _+1
	a = gets.to_i
	if top2.first.a <= a
		top2[0], top2[1] = Pair.new(a, i), top2.first
	elsif top2.last.a < a
		top2[0], top2[1] = top2.first, Pair.new(a, i)
	end
}

(1..(top2.first.i-1)).each{|i|
	p top2.first.a
}
p top2.last.a
((top2.first.i+1)..N).each{|i|
	p top2.first.a
}
