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 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<stdio.h>
#define inf 1072114514
#define size 262144

void prarr(int arr[],int n){
  int i;
  for(i=0;i<n;i++){
    if(i){printf(" ");}
    printf("%d",arr[i]);
  }
  printf("\n");
  return;
}

typedef struct{
int val;
int node;
}data;

int ssize=size;
data vd;

void sinit(data stree[]){
    vd.val=inf;
    vd.node=-1;
    int i,j;
    for(i=0;i<2*ssize;i++){
        stree[i].val=inf;
        stree[i].node=-1;
    }
}

void upd(data stree[],int k,data a){
    int i,m;
    data ld,rd,md;
    k+=ssize-1;
    stree[k]=a;
    while(k>0){
        k=(k-1)/2;
        ld=stree[2*k+1];
        rd=stree[2*k+2];
        if(ld.val<rd.val){md=ld;}
        else{md=rd;}
        stree[k]=md;
    }
}

//calc min[a,b)
//call:k=0,l=0,r=ssize
data query(data stree[],int a,int b,int k,int l,int r){
    data ld,rd;
    if(r<=a || b<=l){return vd;}
    if(a<=l && r<=b){
        return stree[k];
    }
    else{
        ld=query(stree,a,b,k*2+1,l,(l+r)/2);
        rd=query(stree,a,b,k*2+2,(l+r)/2,r);
        if(ld.val<rd.val){return ld;}
        else{return rd;}
    }
}

int n;
data seg[2][2*size];
int res[size];
int mr[size];

void solve(int st,int fi,int pt){
  int i,w,p;
  int fp,sp,tp,lp;
  data fm,sm;
  w=st%2;
  fm=query(seg[w],st,fi+1,0,0,ssize);
  p=fm.node;
  w=1-w;
  sm=query(seg[w],p+1,fi+1,0,0,ssize);
  res[pt]=fm.val;
  res[pt+1]=sm.val;
  pt+=2;
  fp=pt;
  if(st<fm.node-1){solve(st,fm.node-1,fp);}
  pt+=((fm.node-st));
  sp=pt;
  if(fm.node+1<sm.node-1){solve(fm.node+1,sm.node-1,sp);}
  pt+=((sm.node-(fm.node+1)));
  tp=pt;
  if(sm.node+1<fi){solve(sm.node+1,fi,tp);}
  pt+=(fi-sm.node);
  lp=pt;

  int nf=fp,ns=sp,nt=tp,c=0,v;
  while(nf<sp || ns<tp || nt<lp){
    v=inf;
    if(nf<sp){
      if(res[nf]<v){
        v=res[nf];
        w=1;
      }
    }

    if(ns<tp){
      if(res[ns]<v){
        v=res[ns];
        w=2;
      }
    }

    if(nt<lp){
      if(res[nt]<v){
        v=res[nt];
        w=3;
      }
    }

    if(w==1){
      mr[c]=res[nf];
      c++;
      nf++;
      mr[c]=res[nf];
      c++;
      nf++;
    }
    if(w==2){
      mr[c]=res[ns];
      c++;
      ns++;
      mr[c]=res[ns];
      c++;
      ns++;
    }
    if(w==3){
      mr[c]=res[nt];
      c++;
      nt++;
      mr[c]=res[nt];
      c++;
      nt++;
    }
  }
  for(i=0;i<c;i++){
    res[fp]=mr[i];
    fp++;
  }
}

int main(void){
    int i,j,m,k,a[size],b,c,h,w,r=0,l,t;
    data pd;
    sinit(seg[0]);
    sinit(seg[1]);
    scanf("%d",&n);
    for(i=0;i<n;i++){
      scanf("%d",&a[i]);
      if(i%2==0){
        pd.val=a[i];
        pd.node=i;
        upd(seg[0],i,pd);
        pd.val=inf;
        upd(seg[1],i,pd);
      }
      else{
        pd.val=a[i];
        pd.node=i;
        upd(seg[1],i,pd);
        pd.val=inf;
        upd(seg[0],i,pd);
      }
    }
    //printf("ok\n");
    solve(0,n-1,0);
    prarr(res,n);
    return 0;
}