#include <bits/stdc++.h>
#define REP(i,n) for(LL i=0;i<(LL)(n);i++)
#define ALL(x) (x).begin(),(x).end()
#define LL long long
#define pii pair<int,int>
#define Map map

using namespace std;


struct SegmentTree{

    vector<int> data;
    vector<int> odd;
    vector<int> even;
    int n=1;
    int inf = INT_MAX;

    SegmentTree(int n_){
        while(n<n_)n*=2;
        data.resize(n*2-1);
        odd.resize(n*2-1,inf);
        even.resize(n*2-1,inf);
    }

    void add(int a,int x){
        x+=n-1;
        data[x]+=a;
        while(x){
            x=(x-1)/2;
            data[x]=min(data[2*x+1],data[2*x+2]);
        }
    }

    void set(){
        for(int i=n-1;i<2*n;i++){
            ((i-(n-1))%2==0 ? even[i]:odd[i])=data[i];
        }
        for(int i=n-1;i<2*n-1;i++){
            int x=i;
            while(x){
                x=(x-1)/2;
                even[x]=min(even[2*x+1],even[2*x+2]);
                odd[x] =min(odd[2*x+1],  odd[2*x+2]);
            }
        }



    }

    int getMin(int a,int b,int k,int l,int r){

        if(l>=b||r<=a)return inf;
        if(l>=a&&r<=b)return data[k];

        return min(getMin(a,b,2*k+1,l,(r+l)/2), getMin(a,b,2*k+2,(r+l)/2,r));
    }

    //[a,b)
    int getMin(int a,int b){
        return getMin(a,b,0,0,n);
    }


    int getEvenMin(int a,int b,int k,int l,int r){

        if(l>=b||r<=a)return inf;
        if(l>=a&&r<=b)return even[k];

        return min(getEvenMin(a,b,2*k+1,l,(r+l)/2), getEvenMin(a,b,2*k+2,(r+l)/2,r));
    }

    //[a,b)
    int getEvenMin(int a,int b){
        return getEvenMin(a,b,0,0,n);
    }

    int getOddMin(int a,int b,int k,int l,int r){

        if(l>=b||r<=a)return inf;
        if(l>=a&&r<=b)return odd[k];

        return min(getOddMin(a,b,2*k+1,l,(r+l)/2), getOddMin(a,b,2*k+2,(r+l)/2,r));
    }

    //[a,b)
    int getOddMin(int a,int b){
        return getOddMin(a,b,0,0,n);
    }

};

int main(){

    LL N;
    cin>>N;
    int p[N];
    Map<int,int> mp;
    Map<int,pii> range;
    REP(i,N)cin>>p[i];
    REP(i,N)mp[p[i]]=i;
    SegmentTree sg(N);
    REP(i,N)sg.add(p[i],i);
    sg.set();

    priority_queue<int,vector<int>,greater<int> > que;
    que.push(sg.getEvenMin(0,N));
    range[sg.getEvenMin(0,N)]={0,N};

    vector<int> ans;
    while(!que.empty()){
        int p1 = que.top();que.pop();
        int pos =mp[p1];
        int p2 =(pos%2==0 ? sg.getOddMin(pos+1,range[p1].second):sg.getEvenMin(pos+1,range[p1].second));
        ans.push_back(p1);
        ans.push_back(p2);
        int q;
        if(pos>range[p1].first){
            q = (pos%2==1 ? sg.getOddMin(range[p1].first,pos):sg.getEvenMin(range[p1].first,pos));
            range[q]={range[p1].first,pos};
            que.push(q);
        }
        if(mp[p2]>pos+1){
            q = (pos%2==0 ? sg.getOddMin(pos+1,mp[p2]):sg.getEvenMin(pos+1,mp[p2]));
            range[q]={pos+1,mp[p2]};
            que.push(q);
        }
        if(range[p1].second>mp[p2]+1){
            q = (pos%2==1 ? sg.getOddMin(mp[p2]+1,range[p1].second):sg.getEvenMin(mp[p2]+1,range[p1].second));
            range[q]={mp[p2]+1,range[p1].second};
            que.push(q);
        }
    }

    REP(i,N)cout<<ans[i]<<" ";
    cout<<endl;




    return 0;
}

Measured memory_bytes: 24948