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;

typedef long long int 	ll;
typedef pair<int,int>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 100005
#define nn 1005
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define cases int t;  cin>>t;   while(t--)
#define check(a,n) forn(iiii,int(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int n,tree[10*mm],tree2[10*mm],o[4*mm],e[4*mm],k1;

int mini(int a,int b)
{
    a+=k1,b+=k1;
    int s=1e9;
    while(a<=b)
    {
        if(a%2==1) s=min(tree[a++],s);
        if(b%2==0) s=min(s,tree[b--]);
        a/=2,b/=2;
    }
    return s;
}

void up(int k,int x)
{
    k+=k1;
    tree[k]=x;
    for(k/=2;k>=1;k/=2)
        tree[k]=min(tree[2*k+1],tree[2*k]);
}
int mini2(int a,int b)
{
    a+=k1,b+=k1;
    int s=1e9;
    while(a<=b)
    {
        if(a%2==1) s=min(tree2[a++],s);
        if(b%2==0) s=min(s,tree2[b--]);
        a/=2,b/=2;
    }
    return s;
}

void up2(int k,int x)
{
    k+=k1;
    tree2[k]=x;
    for(k/=2;k>=1;k/=2)
        tree2[k]=min(tree2[2*k+1],tree2[2*k]);
}

int pos[2*mm];
vii pp;

bool comp(ii a,ii b)
{
    return min(a.ff,a.ss)<min(b.ff,b.ss);
}

int main()
{
    fast_io;
    cin>>n;
    forn(i,n)
    {
        int a;
        cin>>a,pos[a]=i;
        if(i%2==0) e[i]=a,o[i]=1e9;
        else o[i]=a,e[i]=1e9;
    }
    k1=n;
    while((k1&-k1)!=k1) o[k1++]=1e9,e[k1++]=1e9;
    forn(i,k1) up(i,o[i]);
    forn(i,k1) up2(i,e[i]);
    forn(i,n)
    {
        int v=pos[i+1];
        if(v==-1||v==n-1) continue;
        if(v%2==0)
        {
            int u=mini(v+1,n-1);
            if(u==1e9) continue;
            up(pos[u],1e9);
            pos[u]=-1;
            up(v,1e9);
            pos[i+1]=-1;
            pp.pb({i+1,u});
        }
        else
        {
            int u=mini2(v+1,n-1);
            if(u==1e9) continue;
            up2(pos[u],1e9);
            pos[u]=-1;
            up2(v,1e9);
            pos[i+1]=-1;
            pp.pb({i+1,u});
        }
    }
    sort(all(pp),comp);
    for(auto i:pp)
        cout<<i.ff<<" "<<i.ss<<" ";
    return 0;
}