#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<P,P> PP;
struct SegmentTree{
    private:
        int n;
        vector<P> node; //セグ木の持つデータの型を入力
        P e = P(50000000,0); //単位元
    public:
        P F(P a,P b){
            if (a.first<=b.first) return a;
            else return b;
        }

        void init(vector<P> v) {
            int sz = v.size();
            n = 1; while(n < sz) n *= 2;
            node.resize(2*n-1, e);

            rep(i,sz) node[i+n-1] = v[i];
            per(i,n-1) node[i] = F(node[2*i+1], node[2*i+2]);

            //rep(i,2*n-1) cout << node[i] << endl;
        }

        void update(int x,P val){
            x += n-1;
            node[x] = val;
            while(x > 0) {
                x =(x-1)/2;
                node[x]=F(node[2*x+1],node[2*x+2]);
            }
        
        }

        P get(int a, int b, int k=0, int l=0, int r=-1) {
            if(r < 0) r = n;
            if(r <= a || b <= l) return e;
            if(a <= l && r <= b) return node[k];

            P vl = get(a, b, 2*k+1, l, (l+r)/2);
            P vr = get(a, b, 2*k+2, (l+r)/2, r);
            return F(vl, vr);
        }
};

int n;
vector<P> ps0,ps1;
int p;
priority_queue<PP,vector<PP>,greater<PP>> que;
SegmentTree s0,s1; 
vector<int> ans;

void upd(){
    P seg = que.top().second, p = que.top().first;
    que.pop();
    int l = seg.first,r = seg.second, m = p.first, i = p.second;
    if (i%2==0) {
        P p_ = s1.get(i+1,r);
        int q=p_.first,j=p_.second;
        ans.push_back(m);ans.push_back(p_.first);
        if (l!=i) que.push(PP(s0.get(l,i),P(l,i)));
        if (i+1!=j) que.push(PP(s1.get(i+1,j),P(i+1,j)));
        if (j+1!=r) que.push(PP(s0.get(j+1,r),P(j+1,r)));
    }else{
        P p_ = s0.get(i+1,r);
        int q=p_.first,j=p_.second;
        ans.push_back(m);ans.push_back(p_.first);
        if (l!=i) que.push(PP(s1.get(l,i),P(l,i)));
        if (i+1!=j) que.push(PP(s0.get(i+1,j),P(i+1,j)));
        if (j+1!=r) que.push(PP(s1.get(j+1,r),P(j+1,r)));
    }
}


void solve(){
    cin >> n;
    rep(i,n){
       cin >> p;
       if (i%2==0){
           ps0.push_back(P(p,i));
           ps1.push_back(P(50000000,0));
       }else{
           ps0.push_back(P(50000000,0));
           ps1.push_back(P(p,i));
       }
       //cout << as[i] << endl;
    }
    s0.init(ps0); s1.init(ps1);
    que.push(PP(s0.get(0,n),P(0,n)));
    rep(i,n/2){
        upd();
    }

    rep(i,n){
        cout << ans[i] << " ";
    }

}



int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
    
}

Measured memory_bytes: 15304