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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

const int MX = 200050;
const int MAXL = 20;

struct RMQ {
	int f[20][MX], n;
	RMQ() {}
	void init(int _n, int *a) {
		n = _n;
		for (int i = 0; i < n; i++) {
			f[0][i] = a[i];
		}
		for (int j = 1; 1<<j <= n; j++) {
			for (int i = 0; i < n; i++) {
				f[j][i] = f[j-1][i];
				if (i + (1<<j-1) < n) {
					f[j][i] = min(f[j][i], f[j-1][i + (1<<j-1)]);
				}
			}
		}
	}
	inline int get(int s, int e) { // Get minimum value between
		// [s, e](both inclusive)
		assert(s <= e && 0 <= s);
		int k = 31-__builtin_clz(e-s+1);
		return min(f[k][s], f[k][e-(1<<k)+1]);
	}
} r0, r1;

int p0[MX], p1[MX], p[MX];
int pos[MX];

struct node {
	int st, ed, P0, P1;
	node(int st = 0, int ed = 0) : st(st), ed(ed) {
		if(st & 1) {
			int k = r1.get(st / 2, ed / 2 - 1);
			P0 = pos[k];
			k = r0.get(P0 / 2 + 1, ed / 2);
			P1 = pos[k];
		}
		else {
			int k = r0.get(st / 2, ed / 2);
			P0 = pos[k];
			k = r1.get(P0 / 2, ed / 2);
			P1 = pos[k];
		}
	}
	bool operator < (const node& rhs) const { return p[P0] > p[rhs.P0]; }
};

int main() {
	int n; scanf("%d", &n);
	for(int i = 0; i < n / 2; i++) {
		scanf("%d%d", p0 + i, p1 + i);
		pos[p0[i]] = i * 2; pos[p1[i]] = i * 2 + 1;
		p[i * 2] = p0[i]; p[i * 2 + 1] = p1[i];
	}
	r0.init(n / 2, p0); r1.init(n / 2, p1);
	priority_queue<node> pq;
	pq.push(node(0, n - 1));
	vector<int> res;
	while(pq.size()) {
		node nd = pq.top(); pq.pop();
		if(nd.st < nd.P0) pq.push(node(nd.st, nd.P0 - 1));
		if(nd.P0 + 1 < nd.P1) pq.push(node(nd.P0 + 1, nd.P1 - 1));
		if(nd.P1 < nd.ed) pq.push(node(nd.P1 + 1, nd.ed));
		res.push_back(p[nd.P0]);
		res.push_back(p[nd.P1]);
	}
	for(int i = 0; i < res.size(); i++) printf("%d%c", res[i], i == res.size() - 1 ? '\n' : ' ');
	return 0;
}