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<iostream>
#include<string>
#include<queue>
#include<vector>
using namespace std;
const int INF=100000000;
typedef pair<int,int> Q;
typedef pair<Q,Q> Pa;
int N;
Q seg1[300005]={},seg2[300005]={};
Q query1(int a,int b,int k,int l,int r){
//	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
	if(r<=a||b<=l)return Q(INF,INF);
	if(a<=l&&r<=b)return seg1[k];
	else{
		Q v1=query1(a,b,k*2+1,l,(l+r)/2);
		Q v2=query1(a,b,k*2+2,(l+r)/2,r);
		return min(v1,v2);
	}
}
Q query2(int a,int b,int k,int l,int r){
//	cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
	if(r<=a||b<=l)return Q(INF,INF);
	if(a<=l&&r<=b)return seg2[k];
	else{
		Q v1=query2(a,b,k*2+1,l,(l+r)/2);
		Q v2=query2(a,b,k*2+2,(l+r)/2,r);
		return min(v1,v2);
	}
}
Q query(int a,int b,int k,int l,int r){
	if(a%2==0)return query1(a,b,k,l,r);
	return query2(a,b,k,l,r);
}

int main(){
	int K=1;
	int P[100005]={};
	cin>>N;
	while(K<N)K*=2;
	int a2,b2;
	for(int i=0;i<300005;++i){
		seg1[i]=Q(INF,INF);
		seg2[i]=Q(INF,INF);
	}
	for(int i=0;i<N/2;++i){
		cin>>a2>>b2;
		seg1[i*2+K-1]=Q(a2,i*2);
		seg2[i*2+K]=Q(b2,i*2+1);
	}
	int left=K-2,right=K*2-2;
	while(left<right){
		seg1[left]=min(seg1[right],seg1[right-1]);
		seg2[left]=min(seg2[right],seg2[right-1]);
		left-=1;
		right-=2;
	}
//	for(int i=0;i<K*2-1;++i)cout<<seg2[i].first<<" ";
//	cout<<endl;
	priority_queue<Pa,vector<Pa>,greater<Pa> > que;

	que.push(Pa(query(0,N,0,0,K),Q(0,N-1)));

	int psize=0;
	while(!que.empty()){
		Pa p=que.top();que.pop();
		int a1=p.first.first,b1=p.first.second,c1=p.second.first,d1=p.second.second;
//cout<<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
		P[psize]=a1;
		psize++;
		Q q=query(b1+1,d1+1,0,0,K);
		int e=q.first,f=q.second;
//		cout<<e<<" "<<f<<endl;
		P[psize]=e;
		psize++;
		if(psize==N)break;
		if(c1<b1-1)que.push(Pa(query(c1,b1,0,0,K),Q(c1,b1-1)));
		if(b1+1<f-1)que.push(Pa(query(b1+1,f,0,0,K),Q(b1+1,f-1)));
		if(f+1<d1)que.push(Pa(query(f+1,d1+1,0,0,K),Q(f+1,d1)));

	}
	for(int i=0;i<N-1;++i){
		cout<<P[i]<<" ";
	}
	cout<<P[N-1]<<endl;
	return 0;
}