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: Wrong Answer)

#include "bits/stdc++.h"
#define ll long long
#define rep2(i,a,b) for(int i=(a);i<=(b);++i)
#define rep(i,n) rep2(i,0,n-1)
int MOD=1000000000+7;
int INF=1e6;
 
using namespace std;
string alphabet("abcdefghijklmnopqrstuvwxyz");
 
int a[800000];
 
struct section{
    int first;
    int last;
    int min;
  	
};
bool operator> (const section &s1,const section &s2){
    return s1.min>s2.min;
};
 
 
int d1[800000],d2[800000];
priority_queue<section,vector<section>,greater<section>> p;
 
int search1(int first,int last,int k,int l,int r){
    if(r<first||l>last) return INF;
    if(first<=l&&last>=r) return d1[k];
    int leftmin=search1(first,last,k*2+1,l,(l+r)/2);
    int rightmin=search1(first,last,k*2+2,(l+r+1)/2,r);
    return min(leftmin,rightmin);
}
int search2(int first,int last,int k,int l,int r){
    if(r<first||l>last) return INF;
    if(first<=l&&last>=r) return d2[k];
    int leftmin=search2(first,last,k*2+1,l,(l+r)/2);
    int rightmin=search2(first,last,k*2+2,(l+r+1)/2,r);
    return min(leftmin,rightmin);
}
int n;
int pos[210000];
void GO(){
    while(!p.empty()){
        section s=p.top();
        p.pop();
        int min2;
        if(pos[s.min]%2){
            min2=search2(pos[s.min]+1,s.last,0,0,n-1);
        }
        else{
            min2=search1(pos[s.min]+1,s.last,0,0,n-1);
        }
      	section t;
        if(s.first!=pos[s.min]){
            if(s.first%2) 
            t={s.first,pos[s.min]-1,search1(s.first,pos[s.min]-1,0,0,n-1)};
            else
            t={s.first,pos[s.min]-1,search2(s.first,pos[s.min]-1,0,0,n-1)};
            p.push(t);
        }
        if(pos[s.min]+1!=pos[min2]){
            if(pos[s.min]%2)
            t={pos[s.min]+1,pos[min2]-1,search2(pos[s.min]+1,pos[min2]-1,0,0,n-1)};
            else
            t={pos[s.min]+1,pos[min2]-1,search1(pos[s.min]+1,pos[min2]-1,0,0,n-1)};
            p.push(t);
        }
        if(pos[min2]!=s.last){
            if(pos[min2]%2)
            t={pos[min2]+1,s.last,search2(pos[min2]+1,s.last,0,0,n-1)};
            else
            t={pos[min2]+1,s.last,search1(pos[min2]+1,s.last,0,0,n-1)};
            p.push(t);
        }
        cout<<s.min<<" "<<min2<<" ";
    }
}
main(){
    cin>>n;
    int sup=1;
    while(sup<n)
    sup*=2;
    rep(i,n){
        cin>>a[i];
        pos[a[i]]=i;
    }
  	rep2(i,n,sup) a[i]=INF;
    rep2(i,sup-1,sup*2-2){
        if(i%2) d2[i]=a[i-sup+1];
        else d1[i]=a[i-sup+1];
    }
    for(int i=sup-2;i>=0;i--){
        d1[i]=min(d1[i*2+1],d1[i*2+2]);
        d2[i]=min(d2[i*2+1],d2[i*2+2]);
    }
    section temp{0,n-1,d2[0]};
  	p.push(temp);
    GO();
    return 0;
}