#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
    os << p.first << " " << p.second;
    return os;
}

template<typename Monoid>
struct SegmentTree{
    using F=function<Monoid(Monoid,Monoid)>;
    int sz;
    vector<Monoid> seg;
    const F f;
    const Monoid gen;
    SegmentTree(int n,const F f,const Monoid &gen):f(f),gen(gen){
        sz=1;
        while(sz<n)sz<<=1;
        seg.assign(2*sz,gen);
    }
    void set(int k,const Monoid &x){
        seg[k+sz]=x;
    }
    void build(){
        for(int k=sz-1;k>0;k--) seg[k]=f(seg[2*k],seg[2*k+1]);
    }
    void update(int k,const Monoid &x){
        k+=sz;
        seg[k]=x;
        while(k>>=1) seg[k]=f(seg[2*k],seg[2*k+1]);
    }
   // [a,b)
    Monoid query(int a,int b){
        Monoid L=gen,R=gen;
        for(a+=sz,b+=sz;a<b;a>>=1,b>>=1){
            if(a&1) L=f(L,seg[a++]);
            if(b&1) R=f(seg[--b],R);
        }
        return f(L,R);
    }
    Monoid operator[](const int &k)const {
       return seg[k+sz];
    }
};
using P=pair<int,int>;
P f(P a,P b){
    return a<b?a:b;
}


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int n;cin>>n;
    vector<int> v(n);
    SegmentTree<P> ev(n,f,P(INF,INF)),od(n,f,P(INF,INF));

    rep(i,n){
        cin>>v[i];
        if(i%2) od.set(i,{v[i],i});
        else    ev.set(i,{v[i],i});
    }
    ev.build();od.build();

    vector<P> val(n/2);
    vector<int> in(n/2,0);
    vector<vector<int>> g(n/2);
    int cnt=0;

    function<int(int,int)> label=[&](int l,int r){
        if(l>=r) return -1;
        P fs,sc;
        if(l%2){
            fs=od.query(l,r);
            sc=ev.query(fs.second,r);
        }else{
            fs=ev.query(l,r);
            sc=od.query(fs.second,r);
        }
        int now=cnt++;
        val[now]=P(fs.first,sc.first);
        int mugen=label(l,fs.second);
        if(mugen>=0){
            g[now].push_back(mugen);
            in[mugen]++;
        }
        mugen=label(fs.second+1,sc.second);
        if(mugen>=0){
            g[now].push_back(mugen);
            in[mugen]++;
        }
        mugen=label(sc.second+1,r);
        if(mugen>=0){
            g[now].push_back(mugen);
            in[mugen]++;
        }
        return now;
    };
    label(0,n);

    vector<int> ans;
    priority_queue<pair<P,int>,vector<pair<P,int>>,greater<pair<P,int>>> que;
    rep(i,n/2){
        if(in[i]==0) que.push({val[i],i});
    }
    while(!que.empty()){
        auto pp=que.top();que.pop();
        ans.push_back(pp.first.first);
        ans.push_back(pp.first.second);
        int now=pp.second;
        for(auto to:g[now]){
            in[to]--;
            if(in[to]==0) que.push({val[to],to});
        }
    }
    rep(i,n) cout<<ans[i]<<(i+1==n?"\n":" ");
    return 0;
}


Measured memory_bytes: 54228