#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)
#define Pr(a) (#a)<<"="<<(a)<<" "
#define pr1(a) cerr<<Pr(a)<<endl;
#define pr2(a,b) cerr<<Pr(a)<<Pr(b)<<endl;
#define pr3(a,b,c) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<endl;
#define pr4(a,b,c,d) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<endl;
#define pr5(a,b,c,d,e) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<endl;
#define pr6(a,b,c,d,e,f) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<Pr(f)<<endl;
#define int long long
#define double long double
using namespace std;
const int N = 100010;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class Seg{
public: 
  
  //遅延用の型  
  struct T{
    int type; //0 - empty   , 1 - update
    int value;
    T():type(0),value(0){}
    T(int type,int value):type(type),value(value){}
  };

  //マージ可能な主データ型
  struct D{
    int value,idx;
    D():value(INF),idx(INF){} /*適切な値にする!!!!!!*/
    D(int value,int idx):value(value),idx(idx){}
    bool operator < (D a)const {return value < a.value;} //merge用
  };
  
  int n;
  vector<D> dat;
  vector<T> td;
  D returnD = D(INF,INF); // 範囲外の時に返す値。
  
  Seg(){n=-1;}
  Seg(int n_){
    n=1;
    while(n<n_)n*=2;
    td.resize(2*n-1,T());
    dat.resize(2*n-1,D());
  }
  
  D merge(D a,D b){return min(a,b);}
  
  void delay(int k,int len){
    return;
    if(td[k].type==0) return;
  }
  
  D update(int k,int x,int idx, int len){
    dat[k].value = x;
    dat[k].idx = idx;
    return dat[k];
  }
  
  //[a,b)の値をx変更　add(a,b,x)
  D query(int a,int b,int x,int idx,bool flg=true,int k=0,int l=0,int r=-1){
    if(r==-1) r=n, assert(a <= n && b <= n);
    if(r<=a||b<=l) return flg? dat[k]:returnD;
    if(a<=l&&r<=b) return flg? update(k, x, idx, r - l):dat[k];
    
    //delay(k, r - l);
    D vl = query(a,b,x,idx,flg,k*2+1,l,(l+r)/2);
    D vr = query(a,b,x,idx,flg,k*2+2,(l+r)/2,r);
    if(flg) dat[k] = merge(vl,vr);
    return merge(vl,vr);
  }
  
  //[a,b)の合計値を得る　find(a,b);
  P find(int a,int b){
    D res = query(a,b,0,0,false);
    return P(res.idx,res.value);
  }
};

void insert(vector<int> &a,vector<int> b){for(int c:b) a.push_back(c);}

struct dat{
  int l, r;
  int lval,rval;
  int flag;
  dat(){};
  dat(int l,int r,int lval,int rval,int flag):l(l),r(r), lval(lval), rval(rval),flag(flag){};
  bool operator < (dat a)const {return P(lval,rval) > P(a.lval,a.rval);}
};

int n;
vector<int> p;
vector<int> solve(Seg &tmpA,Seg &tmpB){
  priority_queue<dat> Q;
  Q.push(dat(0,n-1,0,0,0));
  vector<int> res;
  
  while(!Q.empty()){
    dat t = Q.top(); Q.pop();
    int l = t.l, r = t.r, flag = t.flag;
    Seg &A = flag==0? tmpA:tmpB;
    Seg &B = flag==0? tmpB:tmpA;
    int nl, lval; tie(nl,lval) = A.find(l, r+1);
    int nr, rval; tie(nr,rval) = B.find(nl, r+1);

    
    res.push_back(lval);
    res.push_back(rval);
    auto push=[&tmpA,&tmpB,&Q](int l,int r,int flag){
      if(l > r) return;
      Seg &A = flag==0? tmpA:tmpB;
      Seg &B = flag==0? tmpB:tmpA;
      int idxa,a; tie(idxa,a) = A.find(l, r+1);
      int idxb,b; tie(idxb,b) = B.find(idxa, r+1);
      Q.push(dat(l, r, a, b, flag));
    };
    
    push(nl + 1,nr - 1,!flag);
    push(l,nl-1,flag);
    push(nr + 1,r,flag);
  }
  return res;
}

signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin>>n;

  p.resize(n);
  cin>>p;
  Seg A(n),B(n);
  for(int i=0;i<n;i+=2) A.query(i,i+1,p[i],i);
  for(int i=1;i<n;i+=2) B.query(i,i+1,p[i],i);
  vector<int> ans = solve(A,B);
  //assert((int)ans.size() == n);
  cout<<ans<<endl;
  
  return 0;
}


Measured memory_bytes: 40720