#include <iostream>
#include <bits/stdc++.h>

#define si(x) scanf("%d",&x)

using namespace std;

const int MAXN = 200001;
int n;
vector<int> p, indexx;
int treeEven[MAXN*4] , treeOdd[MAXN*4];

void buildEven(int node, int startt, int endd){
    if(startt == endd){
        if(startt&1){
            treeEven[node] = INT_MAX;
        }
        else{
            treeEven[node] = p[startt];
        }
        return;
    }
    buildEven(2*node + 1, startt, (startt+endd)/2);
    buildEven(2*node + 2, (startt+endd)/2 + 1, endd);
    treeEven[node] = min(treeEven[2*node + 1], treeEven[2*node + 2]);
}

int queryMinEven(int node, int startt, int endd, int left, int right){
    if(left > endd || right < startt){
        return INT_MAX;
    }
    if(startt>=left && endd<=right){
        return treeEven[node];
    }
    int leftMin = queryMinEven(2*node+1,startt,(startt+endd)/2,left,right);
    int rightMin = queryMinEven(2*node+2,(startt+endd)/2+1,endd,left,right);
    return min(leftMin,rightMin);
}


void buildOdd(int node, int startt, int endd){
    if(startt == endd){
        if(startt&1){
            treeOdd[node] = p[startt];
        }
        else{
            treeOdd[node] = INT_MAX;
        }
        return;
    }
    buildOdd(2*node + 1, startt, (startt+endd)/2);
    buildOdd(2*node + 2, (startt+endd)/2 + 1, endd);
    treeOdd[node] = min(treeOdd[2*node + 1], treeOdd[2*node + 2]);
}

int queryMinOdd(int node, int startt, int endd, int left, int right){
    if(left > endd || right < startt){
        return INT_MAX;
    }
    if(startt>=left && endd<=right){
        return treeOdd[node];
    }
    int leftMin = queryMinOdd(2*node+1,startt,(startt+endd)/2,left,right);
    int rightMin = queryMinOdd(2*node+2,(startt+endd)/2+1,endd,left,right);
    return min(leftMin,rightMin);
}

int main()
{

    si(n);
    p.resize(n);
    indexx.resize(n+1);
    for(int i=0;i<n;i++){
        si(p[i]);
        indexx[p[i]] = i;
    }
    buildEven(0,0,n-1);
    buildOdd(0,0,n-1);
    vector<int> q(n);
    int ind = 0;
    priority_queue<pair<int,pair<pair<int,int>, int > > > pq;
    pq.push({-1*queryMinEven(0,0,n-1,0,n-1),{{0,n-1},0}});
    while(!pq.empty()){
        pair<int,pair<pair<int,int> ,int > > interval = pq.top();
        interval.first*=-1;
        pq.pop();
        int val;
        bool flag = interval.second.second;
        if(!flag){
            val = queryMinOdd(0,0,n-1,indexx[interval.first] + 1,interval.second.first.second);
        }
        else{
            val = queryMinEven(0,0,n-1,indexx[interval.first] + 1,interval.second.first.second);
        }

        q[ind] = interval.first;
        q[ind + 1] = val;
        ind+=2;

        if(interval.second.first.first < indexx[interval.first]){
            int value;
            if(!flag){
                value = queryMinEven(0,0,n-1,interval.second.first.first,indexx[interval.first] - 1);
            }
            else{
                value = queryMinOdd(0,0,n-1,interval.second.first.first,indexx[interval.first] - 1);
            }
            pq.push({-1*value,{{interval.second.first.first,indexx[interval.first] - 1},flag}});
        }

        if(interval.second.first.second > indexx[val]){
            int value;
            if(!flag){
                value = queryMinEven(0,0,n-1,indexx[val] + 1,interval.second.first.second);
            }
            else{
                value = queryMinOdd(0,0,n-1,indexx[val] + 1,interval.second.first.second);
            }
            pq.push({-1*value,{{indexx[val] + 1 , interval.second.first.second},flag}});
        }

        if(indexx[interval.first] + 1 != indexx[val]){
            int value;
            if(!flag){
                value = queryMinOdd(0,0,n-1,indexx[interval.first] + 1 , indexx[val] - 1);
            }
            else{
                value = queryMinEven(0,0,n-1,indexx[interval.first] + 1 , indexx[val] - 1);
            }
            pq.push({-1*value,{{indexx[interval.first] + 1, indexx[val] - 1},!flag}});
        }
    }
    for(int i=0;i<n;i++){
        cout << q[i];
        if(i!=n-1)cout << " ";
    }
    return 0;
}


Measured memory_bytes: 10740