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>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) //cerr << #x << " : " << x << '\n';
//#define int ll
using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265359;

const ll MOD = (ll) 1e9 + 7;
const ll MAXN = (ll) 4e5 + 10;
const ll INF = (ll) 2242545357980376863;
const ld EPS = (ld) 1e-8;
const ll LOG = (ll) 25;

ll n, fen[MAXN], ind[MAXN], a[MAXN], mk[MAXN];

void add(ll x){
	x = n + 1 - x;
	ll v = x;
	for(; x < MAXN; x += (x & (-x))) fen[x] = max(fen[x], v);
}
ll get(ll x){
	ll res = 0;
	x = n + 1 - x;
	for(; x > 0; x -= (x & (-x))) res = max(res, fen[x]);
	return n + 1 - res;
}

ll fen2[MAXN];
void add2(ll x){
	ll v = x;
	for(; x < MAXN; x += (x & (-x))) fen2[x] = max(fen2[x], v);
}
ll get2(ll x){
	ll res = 0;
	for(; x > 0; x -= (x & (-x))) res = max(res, fen2[x]);
	debug(res);
	return res;
}



void pick(pll a){
	if(max(a.F, a.S) > n + 10){
		return ;
		exit(0);
	}
	debug(a.S);
	add(ind[a.F]);
	add(ind[a.S]);
	add2(ind[a.F]);
	add2(ind[a.S]);
	mk[a.F] = mk[a.S] = 1;
	cout << a.F << " " << a.S << " ";
}


ll a1[MAXN], RMQ1[LOG][MAXN];
void build1(){
	memset(RMQ1, 31, sizeof RMQ1);
	for(int i = 1; i <= n; i++) RMQ1[0][i] = a1[i];
	ll st;
	for(int l = 1; l < LOG; l++){
		st = (1ll << (l - 1));
		for(int i = 1; i <= n; i++){
			RMQ1[l][i] = min(RMQ1[l - 1][i], RMQ1[l - 1][ min(i + st, n) ]); 
		}
	}
}
ll query1(ll l, ll r){
	debug(l); debug(r);
	ll h = r - l;
	assert(h > 0);
	ll res = INF;
	for(int i = 0; i < LOG; i++) if(h & (1ll << i)){
		res = min(res, RMQ1[i][l]);
		l += (1ll << i);
	}
	return res;
}


ll a2[MAXN], RMQ2[MAXN][LOG];
void build2(){
	memset(RMQ2, 31, sizeof RMQ2);
	for(int i = 1; i <= n; i++) RMQ2[0][i] = a2[i];
	ll st;
	for(int l = 1; l < LOG; l++){
		st = (1ll << (l - 1));
		for(int i = 1; i <= n; i++){
			RMQ2[l][i] = min(RMQ2[l - 1][i], RMQ2[l - 1][ min(i + st, n) ]); 
		}
	}
}
ll query2(ll l, ll r){
	ll h = r - l;
	ll res = INF;
	for(int i = 0; i < LOG; i++) if(h & (1ll << i)){
		res = min(res, RMQ2[i][l]);
		l += (1ll << i);
	}
	return res;
}


int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	memset(a1, 31, sizeof a1);
	memset(a2, 31, sizeof a2);
	
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		if(i & 1) a1[i] = a[i];
		else a2[i] = a[i];
		ind[a[i]] = i;
	}
	build1();
	build2();
	ll id, g,g2;
	for(int i = 1; i <= n; i++){
		debug(i);
		if(mk[i]) continue;
		id = ind[i];
		g = get(id);
		g2 = get2(id);
		//debug(g); debug(g2); debug(id);
		if(g <= id || g2 >= id) continue;
		if((g - id) % 2 == 1){
			// F
			if(id & 1) pick({query2(g2 + 1, id), i});
			else pick({query1(g2 + 1, id), i});
		} else {
			// S
			if(id & 1) pick({i, query2(id + 1, g)});
			else pick({i, query1(id + 1, g)});
		}
	}
	return 0;
}

/*

                                 ____              ,----..               ,----..
   ,---,                       ,'  , `.           /   /   \             /   /   \
  '  .' \                   ,-+-,.' _ |          /   .     :           /   .     :
 /  ;    '.              ,-+-. ;   , ||         .   /   ;.  \         .   /   ;.  \
:  :       \            ,--.'|'   |  ;|        .   ;   /  ` ;        .   ;   /  ` ;
:  |   /\   \          |   |  ,', |  ':        ;   |  ; \ ; |        ;   |  ; \ ; |
|  :  ' ;.   :         |   | /  | |  ||        |   :  | ; | '        |   :  | ; | '
|  |  ;/  \   \        '   | :  | :  |,        .   |  ' ' ' :        .   |  ' ' ' :
'  :  | \  \ ,'        ;   . |  ; |--'         '   ;  \; /  |        '   ;  \; /  |
|  |  '  '--'          |   : |  | ,             \   \  ',  /          \   \  ',  /
|  :  :                |   : '  |/               ;   :    /            ;   :    /
|  | ,'                ;   | |`-'                 \   \ .'              \   \ .'
`--''                  |   ;/                      `---`                 `---`
                       '---'

*/