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 <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

#define INF_LL (ll)1e18
#define INF (int)1e9
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
using ll = long long;
using PII = pair<int, int>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

class LazySegTree{
private:
	int n;
	vector<pair<ll, ll> > node, node2[2];
	vector<bool> lazyFlag;
public:
	LazySegTree(){}
	LazySegTree(vector<ll> v){
		int sz = v.size();
		n = 1; while(n < sz) n *= 2;
		node.resize(2*n-1, {INF_LL, INF_LL});
		REP(i, 2)
			node2[i].resize(2*n-1, {INF_LL, INF_LL});
		lazyFlag.resize(2*n-1, false);
		REP(i, sz) node[i+n-1] = {v[i], i};
		REP(i, 2)
			REP(j, sz){
				if(j % 2 == i){
					node2[i][j+n-1] = {v[j], j};
				}else{
					node2[i][j+n-1] = {INF_LL, INF_LL};
				}
			}
		for(int i = n-2;i >= 0;i--) node[i] = min(node[2*i+1],node[2*i+2]);
		REP(j, 2)
			for(int i = n-2;i >= 0;i--) node2[j][i] = min(node2[j][2*i+1],node2[j][2*i+2]);
		
	}


	/*
	void add(int a, int b, ll x, int k=0, int l=0, int r=-1){
		if(r < 0) r = n;
		eval(k, l, r);
		if(b <= l || r <= a) return;

		if(a <= l && r <= b){
			lazy[k] += x;
			lazyFlag[k] = true;
			eval(k, l, r);
		}else{
			add(a, b, x, k*2+1, l, (l+r)/2);
			add(a, b, x, k*2+2, (l+r)/2, r);
			node[k] = node[k*2+1] + node[k*2+2];
		}
	}
	*/

	void update(int k, ll x){
		ll v = k+n-1;
		node[v] = {x, k};
		node2[0][v] = {x, k};
		node2[1][v] = {x, k};
		while(v > 0){
			v = (v-1)/2;
			node[v] = min(node[v*2+1], node[v*2+2]);
			node2[0][v] = min(node2[0][v*2+1], node2[0][v*2+2]);
			node2[1][v] = min(node2[1][v*2+1], node2[1][v*2+2]);
		}
	}

	pair<ll, ll> query(int a, int b, int k=0, int l=0, int r=-1){
		if(r < 0) r = n;

		if(b <= l || r <= a) return {INF_LL, INF_LL};
		if(a <= l && r <= b) return node[k];

		return min(query(a, b, k*2+1, l, (l+r)/2), query(a, b, k*2+2, (l+r)/2, r));
	}

	pair<ll, ll> query2(int a, int b, int type, int k = 0, int l = 0, int r = -1){
		if(r < 0) r = n;
		if(b <= l || r <= a) return {INF_LL, INF_LL};
		if(a <= l && r <= b) return node2[type][k];

		return min(query2(a, b, type, k*2+1, l, (l+r)/2), query2(a, b, type, k*2+2, (l+r)/2, r));
	}
};

using PLL = pair<ll, ll>;


int f[101][101] = {};
vector<pair<int, PLL> > res;
int num;
LazySegTree sg;

int dfs(int l, int r){
	int d = INF;
	while(1){
		PLL mini = sg.query(l, r);
		if(mini.first == INF_LL) break;;

		if(sg.query2(mini.second+1, r, (mini.second+1)%2).first != INF_LL){
			PLL nxt = sg.query2(mini.second+1, r, (mini.second+1)%2);
			sg.update(mini.second, INF_LL);
			sg.update(nxt.second, INF_LL); 

			int a = dfs(mini.second+1, nxt.second);
			res.push_back({a, {mini.first, nxt.first}});
			d = min(a, d);
		}else{
			PLL bef = sg.query2(l, mini.second, (mini.second+1)%2);
			if(bef.first == INF_LL) break;;
			sg.update(mini.second, INF_LL);
			sg.update(bef.second, INF_LL);

			int a = dfs(bef.second+1, mini.second);
			res.push_back({a, {bef.first, mini.first}});
			d = min(a, d);
		}
	}
	return d-1;
}

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	vector<ll> p;

	bool used[214514] = {};

	cin >> N;
	num = N;
	REP(i, N){
		int a;
		cin >> a;
		p.push_back(a);
	}
	sg = LazySegTree(p);

	dfs(0, N);
	
	sort(all(res));
	REP(i, res.size()){
		cout << res[i].second.first << " " << res[i].second.second << " ";
	}
	cout << endl;
}