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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pip;
typedef pair<pll, pll> P;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

template <typename T>
struct SegTree {
	T init_val;
	int n;
	vector<T> dat;
	
	SegTree(int _n, T init_val) : init_val(init_val) {
		n = 1;
		while (n < _n) n *= 2;
		dat.resize(n * 2 - 1, init_val);
	}
	
	void update(int i, T x) {
		i += n - 1;
		dat[i] = min(dat[i], x);
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
	}
	
	void set(int i, T x) {
		dat[i + n - 1] = x;
		update(i, x);
	}
	
	// 探索範囲[a,b), 現在見ているkノード[l,r)
	T query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		
		if (r <= a || b <= l) return init_val;
		if (a <= l && r <= b) return dat[k];
		return min( query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r) );
	}
};

int N;
int p[212345], ans[212345];

int main() {
	cin >> N;
	REP(i, N) scanf("%d", p + i);
	
	if (N == 2) {
		printf("%d %d\n", p[0], p[1]);
		return 0;
	}
	
	SegTree<pii> segodd(N, pii(INF, -1)), segeven(N, pii(INF, -1));
	REP(i, N) {
		if (i & 1) segodd.update(i, pii(p[i], i));
		else segeven.update(i, pii(p[i], i));
	}
	
	priority_queue<pip, vector<pip>, greater<pip> > pq;
	if ((N - 1) & 1) pq.push(pip(segeven.query(0, N - 3).fi, pii(0, N - 1)));
	else pq.push(pip(segodd.query(0, N - 3).fi, pii(0, N - 1)));
	
	int ansp = 0;
	while (!pq.empty()) {
		pip now = pq.top(); pq.pop();
		
		int l = now.se.fi, r = now.se.se;
		if (r == -1) {
			ans[ansp++] = p[l];
			ans[ansp++] = p[l + 1];
			continue;
		}
		
		int idx;
		if (r & 1) idx = segeven.query(l, r - 2).se;
		else idx = segodd.query(l, r - 2).se;
		
		
		
		ans[ansp++] = p[idx];
		ans[ansp++] = p[r];
		
		if (idx - l > 1) {
			if (idx - l == 2) pq.push(pip(p[l], pii(l, -1)));
			else {
				int nextnum;
				if ((idx - 1) & 1) nextnum = segeven.query(l, idx - 3).fi;
				else nextnum = segodd.query(l, idx - 3).fi;
				
				pq.push(pip(nextnum, pii(l, idx - 1)));
			}
		}
		
		if (r - idx > 2) {
			if (r - idx == 3) pq.push(pip(p[idx + 1], pii(idx + 1, -1)));
			else {
				int nextnum;
				if ((r - 1) & 1) nextnum = segeven.query(idx + 1, r - 3).fi;
				else nextnum = segodd.query(idx + 1, r - 3).fi;
				pq.push(pip(nextnum, pii(idx + 1, r - 1)));
			}
		}
	}
	
	REP(i, N) printf("%d%c", ans[i], i == N - 1 ? '\n' : ' ');
	
	return 0;
}