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<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int check(pii t1,pii t2){// < ?
  if(t1.first<t2.first)return 1;
  if(t1.first==t2.first){
    if(t1.second<t2.second)return 1;
    else if(t1.second==t2.second)return 2;//same
  }
  return 0;
}

vector<pii> func(int s, int g, vi a){
  vector<pii> ret;
  if(s==g)return ret;
  int i,j,k,m1,m2;
  for(i=m1=s;i<g;i+=2)if(a[m1]>a[i])m1=i;
  for(i=m2=m1+1;i<g;i+=2)if(a[m2]>a[i])m2=i;
  ret.push_back(pii(a[m1],a[m2]));
  vector<pii> t1,t2,t3;
  t1=func(s,m1,a);
  t2=func(m1+1,m2,a);
  t3=func(m2+1,g,a);
  for(i=j=k=0;i<t1.size()||j<t2.size()||k<t3.size();){
    if(i<t1.size() && (j==t2.size()||check(t1[i],t2[j])==1) && (k==t3.size()||check(t1[i],t3[k])==1)){//min:t1
      ret.push_back(t1[i]);
      i++;
    }
    if(j<t2.size() && (i==t1.size()||check(t2[j],t1[i])==1) && (k==t3.size()||check(t2[j],t3[k])==1)){//min:t2
      ret.push_back(t2[j]);
      j++;
    }
    if(k<t3.size() && (j==t2.size()||check(t3[k],t2[j])==1) && (i==t1.size()||check(t3[k],t1[i])==1)){//min:t3
      ret.push_back(t3[k]);
      k++;
    }
  }
  return ret;
}

int main(void) {
  int i,j;
  int n;
  cin>>n;
  vi a(n);
  rep(i,n)cin>>a[i];
  vector<pii> ans;
  ans=func(0,n,a);
  rep(i,ans.size()){
    if(i)cout<<" ";
    cout<<ans[i].first<<" "<<ans[i].second;
  }
  cout<<endl;
}