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>

using namespace std;
const int N = 200022;
const int INF = 1000000000;
struct node {
    int mn, mnInd;
    node() {mn = INF,mnInd = N;}

    node (int mnn , int idx) {mn = mnn,mnInd = idx;}
};

struct interval {
    int l,r;
    pair<int,int> p;

    bool operator < (const interval & x) const {
        return p.first > x.p.first;
    }
};

map<pair<int,int>, vector<interval> > children;
int n,arr[N];
node odd[4*N], even[4*N];
vector <vector<pair<int,int> > > vec;

priority_queue < interval > pq;

void buildOdd(int pos,int l,int r) {
    if(l == r) {
        if(l % 2)
            odd[pos] = node (arr[l],l);
        return;
    }
    int mid = (l+r)/2;
    buildOdd(pos*2,l,mid), buildOdd(pos*2+1,mid+1,r);
    if(odd[pos*2].mn < odd[pos*2+1].mn)
        odd[pos] = odd[pos*2];
    else
        odd[pos] = odd[pos*2+1];
}

void buildEven(int pos,int l,int r) {
    if(l == r) {
        if(l % 2 == 0)
            even[pos] = node (arr[l],l);
        return;
    }
    int mid = (l+r)/2;
    buildEven(pos*2,l,mid), buildEven(pos*2+1,mid+1,r);
    if(even[pos*2].mn < even[pos*2+1].mn)
        even[pos] = even[pos*2];
    else
        even[pos] = even[pos*2+1];
}

node getOdd(int pos,int l,int r,int qLow,int qHigh) {
    if(l > qHigh || r < qLow || qLow>qHigh)
        return node();
    if(l >= qLow && r <= qHigh)
        return odd[pos];
    int mid = (l+r)/2;
    node lf = getOdd(pos*2,l,mid,qLow,qHigh), ri = getOdd(pos*2+1,mid+1,r,qLow,qHigh);
    if(lf.mn < ri.mn)
        return lf;
    return ri;
}

node getEven(int pos,int l,int r,int qLow,int qHigh) {
    if(l > qHigh || r < qLow || qLow > qHigh)
        return node();
    if(l >= qLow && r <= qHigh)
        return even[pos];
    int mid = (l+r)/2;
    node lf = getEven(pos*2,l,mid,qLow,qHigh), ri = getEven(pos*2+1,mid+1,r,qLow,qHigh);
    if(lf.mn < ri.mn)
        return lf;
    return ri;
}

void solve(int l,int r) {
    if(l > r)
        return;
    int ret = 1;
    if(l & 1) {
        node o = getOdd(1,0,n-1,l,r);
        node temp = getEven(1,0,n-1,o.mnInd+1,r);
//        printf("%d %d ",o.mn,temp.mn);
        pq.push(interval{l,r,make_pair(o.mnInd,temp.mnInd)});
//        ret+= solve(l,o.mnInd-1,curr+1);
//        ret+= solve(o.mnInd+1,temp.mnInd-1,curr+1);
//        ret+= solve(temp.mnInd+1,r,curr+1);
    } else {
        node e = getEven(1,0,n-1,l,r);
        node temp2 = getOdd(1,0,n-1,e.mnInd+1,r);
        pq.push(interval{l,r,make_pair(e.mnInd,temp2.mnInd)});
//        ret+= solve(l,e.mnInd-1,curr+1);
//        ret+= solve(e.mnInd+1,temp2.mnInd-1,curr+1);
//        ret+= solve(temp2.mnInd+1,r,curr+1);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&arr[i]);
    buildOdd(1,0,n-1);
    buildEven(1,0,n-1);
    solve(0,n-1);
    while(!pq.empty()) {
        interval curr = pq.top();
        pq.pop();
        printf("%d %d ",arr[curr.p.first],arr[curr.p.second]);
        solve(curr.l,curr.p.first-1);
        solve(curr.p.first+1,curr.p.second-1);
        solve(curr.p.second+1,curr.r);
    }

    return 0;
}