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 <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<P, int> PP;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

#define MAX_N (1<<18)

struct segtree{
    int n, dat[2*MAX_N-1]; //(1<<17) = 130000

    void init(int n_){
        n = 1;
        while(n < n_) n *= 2;
        for(int i = 0; i < 2*n-1; i++) dat[i] = INF;
    }

    void update(int k, int a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = min(dat[k*2+1], dat[k*2+2]);
        }
    }

    //(a,b,0,0,seg.n)で呼ぶ
    int query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return INF;
        if(a <= l && r <= b) return dat[k];
        else{
            int vl = query(a,b,k*2+1,l,(l+r)/2);
            int vr = query(a,b,k*2+2,(l+r)/2,r);
            return min(vl, vr);
        }
    }
}seg1, seg2;

int n;
int p[200000];
int v2p[200000];
int id;
P val[200000];
vector<int> e[200000];

void solve(int st, int en, int par_id) {
    if(st == en) return;
    int pos, pos2;
    if(st&1) {
        pos = seg1.query(st, en, 0, 0, n);
    } else {
        pos = seg2.query(st, en, 0, 0, n);
    }
    pos = v2p[pos];
    if((pos+1)&1) {
        pos2 = seg1.query(pos+1, en, 0, 0, n);
    } else {
        pos2 = seg2.query(pos+1, en, 0, 0, n);
    }
    pos2 = v2p[pos2];
    int now_id = id;
    val[id++] = P(p[pos], p[pos2]);
    if(par_id != -1) {
        e[par_id].push_back(now_id);
    }
    
    solve(st, pos, now_id);
    solve(pos+1, pos2, now_id);
    solve(pos2+1, en, now_id);
}

int main(){
    cin >> n;
    rep(i,n) cin >> p[i];
    rep(i,n) p[i]--;
    rep(i,n) v2p[p[i]] = i;
    seg1.init(n);
    seg2.init(n);
    rep(i,n) {
        if(i&1) {
            seg1.update(i, p[i]);
        } else {
            seg2.update(i, p[i]);
        }
    }
    solve(0, n, -1);
    vector<P> ans;
    ans.push_back(val[0]);
    priority_queue<PP, vector<PP>, greater<PP>> que;
    rep(i,e[0].size()) {
        que.push(PP(val[e[0][i]], e[0][i]));
    }
    while(!que.empty()) {
        PP p = que.top();
        que.pop();
        ans.push_back(p.first);
        rep(i, e[p.second].size()) {
            que.push(PP(val[e[p.second][i]], e[p.second][i]));
        }
    }
    
    rep(i,ans.size()) {
        cout << ans[i].first+1 << " " << ans[i].second+1;
        if(i != ans.size()-1) {
            cout << " ";
        } else {
            cout << endl;
        }
    }
}