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: Runtime Error)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<20)
typedef pair<ll, ll> pll;
vector<ll> even_p, odd_p;
vector<pll> even, odd;

/* RMQ begin */
//dat{ val , index }
int init(int n_,vector<pll>& dat) {
	int n = 1;
	while (n < n_) n *= 2;
	dat.resize(2 * n - 1);
	for (int i = 0; i < 2 * n - 1;i++) dat[i] = { INF,INF };
	return n;
}

// k = p_index
void update(int k, int a,int n,vector<pll>& dat) {
	int temp = k;
	k /= 2;
	k += n - 1;
	dat[k] = { a, temp};

	while (k > 0) {
		k = (k - 1) / 2;
		dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
}
// [a,b)の最小値
pll query(int a, int b, int k, int l, int r,vector<pll>& dat) {
	// [a,b)と[l,r)が交差しなければ,INT_MAX
	if (r <= a || b <= l) return{ INF,INF };
	// [a,b)が[l,r)を完全に含んでいれば,この節点の値
	if (a <= l && r <= b) return dat[k];
	else {
		pll vl = query(a, b, k * 2 + 1, l, (l + r) / 2, dat);
		pll vr = query(a, b, k * 2 + 2, (l + r) / 2, r, dat);
		return min(vl, vr);
	}
}
/* RMQ end */

struct Node {
	// [l,r)
	int l, r;
	// even(odd) index 
	int a, b;
	
	int Min_val, Min_index;
	Node(int l, int r) :l(l), r(r) {
		if (l % 2 == 0) {
			a = l / 2;
			b = r / 2;
			auto x = query(a, b+1, 0, 0, even_p.size(), even);
			Min_val = x.first; Min_index = x.second;
		}
		else {
			a = (l - 1) / 2;
			b = (r - 1) / 2;
			auto x = query(a, b+1, 0, 0, odd_p.size(), odd);
			Min_val = x.first; Min_index = x.second;
		}
	}

	bool operator <(const Node&o) const {
		// 昇順
		return o.Min_val < Min_val;
	}
};

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int N; cin >> N;
	for (int i = 0; i < N;i++) {
		int p; cin >> p;
		if (i % 2 == 0) even_p.push_back(p);
		else odd_p.push_back(p);
	}

	// even/odd initialize
	int even_n = init(even_p.size(), even);
	int odd_n = init(odd_p.size(), odd);

	for (int i = 0; i < N;i++) {
		if (i % 2 == 0) update(i, even_p[i / 2], even_n, even);
		else update(i, odd_p[i / 2], odd_n, odd);
	}

	// solve
	priority_queue<Node> pq;
	pq.push(Node(0, N));

	for (int i = 0; i < N / 2;i++) {
		auto x = pq.top(); pq.pop();
		auto y = Node(x.Min_index + 1, x.r);

		//cout << x.Min_val << " " << y.Min_val << " ";

		if (x.Min_index % 2 == 0)update(x.Min_index, INF, even_n, even);
		else update(x.Min_index, INF, odd_n, odd);
		if (y.Min_index % 2 == 0) update(y.Min_index, INF, even_n, even);
		else update(y.Min_index, INF, odd_n, odd);

		if(x.l != x.Min_index)pq.push(Node(x.l, x.Min_index-1));
		if(x.Min_index+1 != y.Min_index)pq.push(Node(x.Min_index + 1, y.Min_index));
		if(y.Min_index+1 != y.r)pq.push(Node(y.Min_index + 1, y.r));
	}
	cout << endl;
}