from collections import defaultdict
d = defaultdict(int)
n, *a = map(int,open(0))
for i in a:
  d[i] += 1
l = list(d.key())
l.sort()
m = l[-1]
if d[m] == 1:
  i = a.index(m)
  [print(m) for j in range(i)]
  print(l[-2])
  [print(m) for j in range(n-i-1)]
else:
  [print(m) for i in range(n)]