Problem:
Score : 800 points

Problem Statement

Let N be a positive even number.

We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N).
Snuke is constructing another permutation of (1, 2, ..., N), q, following the procedure below.

First, let q be an empty sequence.
Then, perform the following operation until p becomes empty:

Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.

When p becomes empty, q will be a permutation of (1, 2, ..., N).

Find the lexicographically smallest permutation that can be obtained as q.

Constraints

N is an even number.

2 ≤ N ≤ 2 × 10^5

p is a permutation of (1, 2, ..., N).

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print the lexicographically smallest permutation, with spaces in between.

Sample Input 1

4
3 2 4 1

Sample Output 1

3 1 2 4

The solution above is obtained as follows:

p

q

(3, 2, 4, 1)

()

↓

↓

(3, 1)

(2, 4)

↓

↓

()

(3, 1, 2, 4)

Sample Input 2

2
1 2

Sample Output 2

1 2

Sample Input 3

8
4 6 3 2 8 5 7 1

Sample Output 3

3 1 2 7 4 6 8 5

The solution above is obtained as follows:

p

q

(4, 6, 3, 2, 8, 5, 7, 1)

()

↓

↓

(4, 6, 3, 2, 7, 1)

(8, 5)

↓

↓

(3, 2, 7, 1)

(4, 6, 8, 5)

↓

↓

(3, 1)

(2, 7, 4, 6, 8, 5)

↓

↓

()

(3, 1, 2, 7, 4, 6, 8, 5)

Solution in Python (3.4.3) (judge verdict: Time Limit Exceeded)

# E
import heapq

N = int(input())
p_list = list(map(int, input().split()))

INT_MAX = N + 1

K2 = 0
N2 = 1

while N2 < N:
    N2 *= 2
    K2 += 1

# segtree
SGT_0 = []
SGT_1 = []
for k in range(K2+1):
    SGT_0.append([INT_MAX] * (N2 // (2**k)))
    SGT_1.append([INT_MAX] * (N2 // (2**k)))

# update
def update(i, a):
    global SGT_0
    global SGT_1
    if i % 2 == 0:
        j = i
        SGT_0[0][j] = a
        for k in range(1, K2+1):
            j = j // 2
            SGT_0[k][j] = min(SGT_0[k-1][2*j], SGT_0[k-1][2*j+1])
    else:
        j = i
        SGT_1[0][j] = a
        for k in range(1, K2+1):
            j = j // 2
            SGT_1[k][j] = min(SGT_1[k-1][2*j], SGT_1[k-1][2*j+1])
            
# query [a, b)
def query_min_0(a, b, k=K2, l=0, r=N2):
    # min in even numbers in [a, b)
    if r <= a or b <= l:
        return INT_MAX
    elif a <= l and r <= b:
        return SGT_0[k][l // (2**k)]
    else:
        vl = query_min_0(a, b, k-1, l, (l+r)//2)
        vr = query_min_0(a, b, k-1, (l+r)//2, r)
        return min(vl, vr)
    
def query_min_1(a, b, k=K2, l=0, r=N2):
    if r <= a or b <= l:
        return INT_MAX
    elif a <= l and r <= b:
        return SGT_1[k][l // (2**k)]
    else:
        vl = query_min_1(a, b, k-1, l, (l+r)//2)
        vr = query_min_1(a, b, k-1, (l+r)//2, r)
        return min(vl, vr)
    
def query_min_even(a, b, k=K2, l=0, r=N2):
    if a % 2 == 0:
        return query_min_0(a, b, k, l, r)
    else:
        return query_min_1(a, b, k, l, r)
    
def query_min_odd(a, b, k=K2, l=0, r=N2):
    if a % 2 == 1:
        return query_min_0(a, b, k, l, r)
    else:
        return query_min_1(a, b, k, l, r)
    
# initial value
# index_list
ind_list = [0]*(N+1)
for i in range(N):
    update(i, p_list[i])
    ind_list[p_list[i]] = i
    
# heapq
h0 = []
heapq.heapify(h0)
heapq.heappush(h0, (query_min_even(0, N), (0, N)))

q_list = []

for _ in range(N // 2):
    q0, (a, b) = heapq.heappop(h0)
    q_list.append(str(q0))
    c = ind_list[q0]
    q1 = query_min_odd(c, b)
    q_list.append(str(q1))
    d = ind_list[q1]
    if a < c:
        heapq.heappush(h0, (query_min_even(a, c), (a, c)))
    if c + 1 < d:
        heapq.heappush(h0, (query_min_even(c + 1, d), (c + 1, d)))
    if d < b:
        heapq.heappush(h0, (query_min_even(d + 1, b), (d + 1, b)))

print(" ".join(q_list))