n = int(input())
a = list(map(int, input().split()))
max1 = max(a[0], a[1])
max2 = min(a[1], a[0])
for i in range(1, n):
if a[i] >= max2:
print(max2, end=" ")
max1 = max2
max2 = a[i]
else:
if a[i] > max1:
print(max1, end=" ")
max1 = a[i]
else:
print(max1, end=" ")