#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl
 
using namespace std;
 
typedef pair<int,int>P;
 
const int MAX_N = 100005;
 
template<class V> class segtree {
private:
    int n,sz;
    vector<V> node, lazy;
    vector<int> node_id;
    vector<bool> lazyFlag;
 
public:
    segtree(vector<V> v) {
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1);
        node_id.resize(2*n-1);
        lazy.resize(2*n-1, 0);
        lazyFlag.resize(2*n-1,false);
        rep(i,sz){
            node[i+n-1] = v[i];
            node_id[i+n-1] = i;
        }
        for(int i=n-2; i>=0; i--){
            if(node[i*2+1] < node[i*2+2]){
                node[i] = node[i*2+1];
                node_id[i] = node_id[i*2+1];
            }else{
                node[i] = node[i*2+2];
                node_id[i] = node_id[i*2+2];
            }
        }
    }
    void eval(int k, int l, int r) {
        if(lazyFlag[k]) {
            node[k] = lazy[k];
            if(r - l > 1) {
                lazy[k*2+1] = lazy[k*2+2] = lazy[k];
                lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
            }
            lazyFlag[k] = false;
        }
    }
    void range(int a, int b, V x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return;
        }
        if(a <= l && r <= b) {
            lazy[k] = x;
            lazyFlag[k] = true;
            eval(k, l, r);
        }
        else {
            range(a, b, x, 2*k+1, l, (l+r)/2);
            range(a, b, x, 2*k+2, (l+r)/2, r);
            if(node[k*2+1] < node[k*2+2]){
                node[k] = node[k*2+1];
                node_id[k] = node_id[k*2+1];
            }else{
                node[k] = node[k*2+2];
                node_id[k] = node_id[k*2+2];
            }
        }
    }
    pair<V,int> query(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;
        eval(k, l, r);
        if(b <= l || r <= a){
            return pair<V,int>(INT_MAX,-1);
        }
        if(a <= l && r <= b){
            return pair<V,int>(node[k],node_id[k]);
        }
        pair<V,int> vl = query(a, b, 2*k+1, l, (l+r)/2);
        pair<V,int> vr = query(a, b, 2*k+2, (l+r)/2, r);
        return min(vl,vr);
    }
    void print()
    {
        rep(i,sz){
            pair<V,int> p;
            p = query(i,i+1);
            cout << "st[" << i << "]: " << p.fi << " " << p.se << endl;
        }
    }
};
 
struct state
{
    int l,r,mn,pl;
    bool operator<(const state& another) const {
        return mn > another.mn;
    }
};
 
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> ev(n,0),od(n,0);
    rep(i,n){
        if(i%2){
            cin >> od[i];
            ev[i] = INF;
        }else{
            cin >> ev[i];
            od[i] = INF;
        }
    }
    segtree<int> st1(ev),st2(od);
    priority_queue<state> que;
    P p = st1.query(0,n);
    que.push((state){0,n,p.fi,p.se});
    vector<int> res;
    while(!que.empty()){
        state s = que.top();
        que.pop();
        if(s.l == s.r){
            continue;
        }
        if(s.l % 2 == 0){
            P q = st2.query(s.pl+1,s.r);
            res.pb(s.mn),res.pb(q.fi);
            P p1 = st1.query(s.l,s.pl);
            que.push((state){s.l,s.pl,p1.fi,p1.se});
            P p2 = st2.query(s.pl+1,q.se);
            que.push((state){s.pl+1,q.se,p2.fi,p2.se});
            P p3 = st1.query(q.se+1,s.r);
            que.push((state){q.se+1,s.r,p3.fi,p3.se});
        }else{
            P q = st1.query(s.pl+1,s.r);
            res.pb(s.mn),res.pb(q.fi);
            P p1 = st2.query(s.l,s.pl);
            que.push((state){s.l,s.pl,p1.fi,p1.se});
            P p2 = st1.query(s.pl+1,q.se);
            que.push((state){s.pl+1,q.se,p2.fi,p2.se});
            P p3 = st2.query(q.se+1,s.r);
            que.push((state){q.se+1,s.r,p3.fi,p3.se});
        }
    }
    rep(i,n-1){
        cout << res[i] << " ";
    }
    cout << res[n-1] << endl;
    return 0;
}

Measured memory_bytes: 21612