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)

/********************************************

	Title		ARC080E.cpp
	Author 		TomatoLoveCoconut
	Time		2018-3-18
	History		Created 2018-3-18
	
	Version		1.0.0
	
	Tags:		RMQ
			priority_queue


	while(true) RP++;
	
		 QAQ

*********************************************/


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define base 311
#define mod 19260817
#define maxlongint 2147483647
#define maxint 32767
#define pi (double)acos(-1.0)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
//SGT
#define MID (l+r)>>1
#define lson o<<1
#define rson o<<1|1
//clear the array
#define cla(a) memset(a,0,sizeof(a))
#define clb(a) memset(a,INF,sizeof(a));
#define clc(a) memset(a,-1,sizeof(a));
#define maxn 200100


using namespace std;


//complex
struct Complex{
    double x,y;
    Complex(double xx=0,double yy=0){x=xx,y=yy;}
};
inline Complex operator + (Complex a,Complex b){return Complex(a.x+b.x,a.y+b.y);}
inline Complex operator - (Complex a,Complex b){return Complex(a.x-b.x,a.y-b.y);}
inline Complex operator * (Complex a,Complex b){return Complex(a.x*b.x-a.y*b.y,a.y*b.x+b.y*a.x);}
//BIT
inline int lowbit(int x){return x&(-x);}




//var
int f[maxn][18],g[maxn][18];
int a[maxn],b[maxn],c[maxn];
int n;
int logn[maxn];
int M[maxn];
inline int getmin(int tp,int l,int r)
{
	int k=logn[r-l+1];
	if (tp==1)
	{
		return min(f[l][k],f[r-(1<<k)+1][k]);
	}
	else{
		return min(g[l][k],g[r-(1<<k)+1][k]);
	}
}

#define piii pair<int,pii>
priority_queue<piii,vector<piii >,greater<piii > > Q;

int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%d",&n);
	int k=0;
	for (int i=0;i<=n+1;i++)
	{
		if ((1<<(k+1)<=i))++k;
		logn[i]=k;
	}
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		M[a[i]]=i;
		if (i&1)
		{
			b[i]=a[i];
			c[i]=maxlongint;
		}
		else
		{
			c[i]=a[i];
			b[i]=maxlongint;
		}
		f[i][0]=b[i];
		g[i][0]=c[i];
	}
	for (int k=1;(1<<k)<=n;k++)
	{
		for (int i=1;i+(1<<k)-1<=n;i++)
		{
			f[i][k]=min(f[i][k-1],f[i+(1<<(k-1))][k-1]);
			g[i][k]=min(g[i][k-1],g[i+(1<<(k-1))][k-1]);
		}
	}
	int x=getmin(1,1,n);
	Q.push(mp(x,mp(1,n)));
	int cnt=0;
	int l,r;
	while (!Q.empty() && cnt<=n)
	{
		pair<int,pii > xx=Q.top();
		Q.pop();
		int L=xx.second.first;
		int R=xx.second.second;
		int x=xx.first;
		int pos=M[x]+1;
		int y=getmin(pos&1,pos,R);
		cnt+=2;
		cout<<x<<" "<<y<<" ";
		l=M[x];
		r=M[y];
		if (L!=l)
			Q.push(mp(getmin(L&1,L,l-1),mp(L,l-1)));
		if (l+1!=r)
			Q.push(mp(getmin((l+1)&1,l+1,r-1),mp(l+1,r-1)));
		if (r!=R)
			Q.push(mp(getmin((r+1)&1,r+1,R),mp(r+1,R)));
	}
	return 0;

}