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 fi first
#define se second
#define mp make_pair
#define gc getchar
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x1 uvuvwevwevwe
#define y1 onyetenyevwe
#define x2 ugwemubwem
#define y2 ossas
typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
template <typename T> void scan(T &angka){
	angka=0;char input=gc();T kali=1;
	while(!(48<=input&&input<=57)){	if(input=='-')	kali=-1;input=gc();}
	while(48<=input&&input<=57)	angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}
template <typename T1,typename T2> void scan(T1 &angka1,T2 &angka2){
	scan(angka1);scan(angka2);
}
const int MAXN=200000;
const int logn=18;
const int INF=MAXN+1;
int n,perm[MAXN+5],msb[MAXN+5],ans[MAXN+5],idx=1;
priority_queue <pair<int,pii> >pq;
//first itu terkecil, second itu pilihan
pii st[2][logn+2][MAXN+5];
int jenis(int indeks){			//membedakan ganjil genap
	return (indeks&1)?1:0;
}
void buatst(int id,int depth){
	for(int i=1;i<=n;i++)
	{
		if((i+(1<<(depth-1)))>n)
			st[id][depth][i]=st[id][depth-1][i];
		else
		{
			st[id][depth][i]=min(st[id][depth-1][i],st[id][depth-1][i+(1<<(depth-1))]);
		}
	}
}
void basecasest(int id){
	for(int i=1;i<=n;i++)
	{
		if(jenis(i)==id)
			st[id][0][i]=mp(perm[i],i);
		else
			st[id][0][i]=mp(INF,i);
	}
}
void buatst(int id){
	basecasest(id);
	for(int i=1;i<logn;i++)
		buatst(id,i);
}
void buatst(){
	for(int id=0;id<2;id++)
		buatst(id);
}
void isimsb(){
	msb[0]=-1;			//harusnya ini dak mungkin
	msb[1]=0;
	for(int i=2;i<=n;i++)
	{
		msb[i]=msb[i-1];
		while((1<<(msb[i]+1))<=i)				//loopnya harusnya at most 1 kali
			msb[i]++;
	}
}
pii query(int id,int l,int r){
	int panjang=r-l+1;
	return min(st[id][msb[panjang]][l],st[id][msb[panjang]][r-(1<<msb[panjang])+1]);
}
void updateans(int angka){
	ans[idx++]=angka;
}
void masukkan(int l,int r){
	if(l>r)
		return;
	pq.push(mp(-query(jenis(l),l,r).fi,mp(l,r)));
}
pii rangetarget(int l,int r){
	pii ret,temp;
	temp=query(jenis(l),l,r);
	ret.fi=temp.se;
	temp=query(jenis(l)^1,ret.fi,r);
	ret.se=temp.se;
	return ret;
}
void pecah(int l,int r,int ll,int rr){
	masukkan(l,ll-1);
	masukkan(ll+1,rr-1);
	masukkan(rr+1,r);
}
void carians(){
	masukkan(1,n);
	while(!pq.empty())
	{
		pii range=pq.top().se;
		pq.pop();
		pii pilihan=rangetarget(range.fi,range.se);
		updateans(perm[pilihan.fi]);
		updateans(perm[pilihan.se]);
		pecah(range.fi,range.se,pilihan.fi,pilihan.se);
	}
}
int main()
{
	scan(n);
	for(int i=1;i<=n;i++)
		scan(perm[i]);
	buatst();
	isimsb();
	carians();
	for(int i=1;i<=n;i++)
	{
		printf("%d",ans[i]);
		if(i==n)
			printf("\n");
		else
			printf(" ");
	}
}