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 (Python):

N = int(input())
*P, = map(int, input().split())
M = [0]*(N+1)
for i, p in enumerate(P):
    M[p] = i

INF = 10**9
def init(P, n):
    n0 = 2 ** (n-1).bit_length()
    data = [INF]*(n0*2)
    data[n0-1:n0+n-1] = P
    for i in range(n0-2, -1, -1):
        data[i] = min(data[2*i+1], data[2*i+2])
    return data

"""
def __get(data, a, b, k, l, r):
    if a <= l and r <= b:
        return data[k]
    if b <= l or r <= a:
        return INF
    vl = __get(data, a, b, 2*k+1, l, (l+r)//2)
    vr = __get(data, a, b, 2*k+2, (l+r)//2, r)
    return min(vl, vr)
"""

def get_data(data, l, r):
    n0 = len(data)//2
    l += n0
    r += n0
    while l < r:
        if r&1:
            r -= 1
            yield data[r-1]
        if l&1:
            yield data[l-1]
            l += 1
        l >>= 1
        r >>= 1
    yield INF

def get(data, l, r):
    
    return min(get_data(data, l, r))

d0 = init(P[0::2], N//2)
d1 = init(P[1::2], N//2)

def query_x(l, r):
    if l % 2 == 0:
        x = get(d0, l//2, r//2)
    else:
        x = get(d1, l//2, r//2)
    return x
def query_y(l, r):
    if l % 2 == 0:
        y = get(d1, l//2, r//2)
    else:
        y = get(d0, (l+1)//2, (r+1)//2)
    return y

from heapq import heappush, heappop
que = [(query_x(0, N), 0, N)]
ans = []
while que:
    x, l, r = heappop(que)
    if l+2 < r:
        xi = M[x]
        y = query_y(xi, r)

        yi = M[y]

        
        if l < xi:
            heappush(que, (query_x(l, xi), l, xi))
        
        if xi+1 < yi:
            heappush(que, (query_x(xi+1, yi), xi+1, yi))
        
        if yi+1 < r:
            heappush(que, (query_x(yi+1, r), yi+1, r))
    else:
        y = P[r-1]

    ans.append("%d %d" % (x, y))
print(*ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
gosa = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

class Seg():
    def __init__(self, na, default, func):
        if isinstance(na, list):
            n = len(na)
        else:
            n = na
        i = 1
        while 2**i <= n:
            i += 1
        self.D = default
        self.H = i
        self.N = 2**i
        if isinstance(na, list):
            self.A = [default] * (self.N) + na + [default] * (self.N-n)
            for i in range(self.N-1,0,-1):
                self.A[i] = func(self.A[i*2], self.A[i*2+1])
        else:
            self.A = [default] * (self.N*2)
        self.F = func

    def find(self, i):
        return self.A[i + self.N]

    def update(self, i, x):
        i += self.N
        self.A[i] = x
        while i > 0:
            i = (i-1) // 2
            self.A[i] = self.merge(self.A[i*2], self.A[i*2+1])

    def merge(self, a, b):
        return self.F(a, b)

    def total(self):
        return self.A[0]

    def query(self, a, b):
        A = self.A
        l = a + self.N
        r = b + self.N
        res = self.D
        while l < r:
            if l % 2 == 1:
                res = self.merge(res, A[l])
                l += 1
            if r % 2 == 1:
                r -= 1
                res = self.merge(res, A[r])
            l >>= 1
            r >>= 1

        return res

def main():
    n = I()
    a = LI()

    def sf(a,b):
        if a < b:
            return a
        return b

    s1 = Seg([c if i%2==0 else inf for c,i in zip(a,range(n))],inf,sf)
    s2 = Seg([c if i%2==1 else inf for c,i in zip(a,range(n))],inf,sf)
    d = {}

    for i in range(n):
        d[a[i]] = i

    def f(l,r):
        if l % 2 == 0:
            t = s1.query(l,r)
            ti = d[t]
            u = s2.query(ti+1,r)
        else:
            t = s2.query(l,r)
            ti = d[t]
            u = s1.query(ti+1,r)
        ui = d[u]
        nl = []
        if l < ti:
            nl.append((l, ti))
        if ui - ti > 1:
            nl.append((ti+1, ui))
        if ui < r-1:
            nl.append((ui+1, r))
        return ((t,u), nl)

    q = []
    heapq.heappush(q, f(0,n))
    r = []
    while q:
        t,nl = heapq.heappop(q)
        r.append(t[0])
        r.append(t[1])
        for t,u in nl:
            heapq.heappush(q, f(t,u))

    return ' '.join(map(str,r))

print(main())