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;

#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll; typedef vector<int> VI; typedef pair<int,int> pii;
const ll mod = 1e9+7;

struct Segtree{
	typedef ll sgT;
	vector<sgT> seg;
	int nbn; sgT INF;
	void init(int n){
		for(nbn=1; nbn<n+2; nbn<<=1); INF=1LL<<29; // min:+inf,max:-inf
		seg.assign(nbn*2, INF);
	}
	sgT comp(sgT l, sgT r){return min(l, r);}  //min<->max
	void update(int k,sgT x){
		k+=nbn-1; seg[k]=x;
		while(k>0){ k=(k-1)/2; seg[k]=comp(seg[k*2+1],seg[k*2+2]); }
	}
	sgT query2(int a,int b,int l,int r,int k){
		if(b<=l||r<=a) return INF;
		if(a<=l&&r<=b) return seg[k];
		return comp(query2(a,b,l,(l+r)/2,k*2+1), query2(a,b,(l+r)/2,r,k*2+2));
	}
	sgT query(int a,int b){ return query2(a,b,0,nbn,0); }
	sgT get(int a){return seg[nbn+a-1];}  // =query(a,a+1)
};

struct Sts{
	int v,l,r,a,b;
	Sts(int v,int l,int r,int a,int b):v(v),l(l),r(r),a(a),b(b){}
	bool operator>(const Sts& b) const {
		return v > b.v; 
	}

};

Segtree sgg, sgk;


int gtps(int l, int r, int v,  Segtree& sgt){
	
	int lo=l-1, hi=r;
	while(hi - lo >1){
		int md = (hi + lo) >> 1;
		if( sgt.query(lo, md+1) <= v ) hi = md; else lo = md; 
	}

	return hi;
}

Sts f(int l, int r){
	//cout <<"l="<<l <<"  ";
	int v,a,b;
	if(l%2){
		v = sgk.query(l,r);
		a = gtps(l,r,v,sgk);
		int t = sgg.query(a+1,r);
		b = gtps(a+1,r,t,sgg);
	}else{
		v = sgg.query(l,r);
		a = gtps(l,r,v,sgg);
		int t = sgk.query(a+1,r);
		b = gtps(a+1,r,t,sgk);
	}
	
	return Sts(v,l,r,a,b);
}

int main()
{
	//cin.tie(0); ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	VI p(n);
	sgg.init(n+2); sgk.init(n+2);

	rep(i,n){
		int c; cin >>c;
		p[i] = c;
		if(i%2) sgk.update(i,c);
		else sgg.update(i,c);
	}
	Sts sta = f(0,n);
	priority_queue<Sts, vector<Sts>, greater<Sts> > pq;
	pq.push(sta);
	VI ans;
	while(!pq.empty()){
		auto cr = pq.top(); pq.pop();
		//printf("%d:%d:%d:%d:%d   ",cr.v,cr.l,cr.r,cr.a,cr.b);
		//if(cr.v > n) continue;
		ans.push_back(p[cr.a]);
		ans.push_back(p[cr.b]);
		if(cr.a - cr.l > 1){
			auto nx = f(cr.l, cr.a);
			pq.push(nx);
		}
		if(cr.b - cr.a > 1){
			auto nx = f(cr.a+1, cr.b);
			pq.push(nx);
		}
		if(cr.r - cr.b > 1){
			auto nx = f(cr.b+1, cr.r);
			pq.push(nx);
		}
	}
	
	for(auto x :ans) printf("%d ",x); puts("");
	return 0;
}