#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

struct RMinQ{
private:
    int N;
    vector<long long> node, lazy;
    vector<bool> lazyFlg;
    long long DEFAULT;
public:
    void init(int n, long long def=9223372036854775807LL){
        DEFAULT = def;
        node.clear();
        lazy.clear();
        lazyFlg.clear();
        N = 1;
        while(N < n) N = (N<<1);
        for(int i=0; i<2*N-1; i++){
            node.push_back(DEFAULT);
            lazy.push_back(0LL);
            lazyFlg.push_back(false);
        }
    }
    void eval(int k, int l, int r){
        if(lazyFlg[k]){
            node[k] = lazy[k];
            if(r-l > 1){
                lazy[(k<<1)+1] = lazy[k];
                lazyFlg[(k<<1)+1] = true;
                lazy[(k<<1)+2] = lazy[k];
                lazyFlg[(k<<1)+2] = true;
            }
            lazy[k] = 0LL;
            lazyFlg[k] = false;
        }
        else{
            node[k] += lazy[k];
            if(r-l > 1){
                lazy[(k<<1)+1] += lazy[k];
                lazy[(k<<1)+2] += lazy[k];
            }
            lazy[k] = 0LL;
        }
    }
    void update(int a, long long x){
        update(a, a+1, x);
    }
    void update(int a, int b, long long x, int k=0, int l=0, int r=-1){
        if(a >= b) return;
        if(r == -1) r = N;
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b){
            lazy[k] = x;
            lazyFlg[k] = true;
            eval(k, l, r);
        }
        else{
            update(a, b, x, (k<<1)+1, l, (l+r)>>1);
            update(a, b, x, (k<<1)+2, (l+r)>>1, r);
            node[k] = std::min(node[(k<<1)+1], node[(k<<1)+2]);
        }
    }
    void add(int a, long long x){
        add(a, a+1, x);
    }
    void add(int a, int b, long long x, int k=0, int l=0, int r=-1){
        if(a >= b) return;
        if(r == -1) r = N;
        eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b){
            lazy[k] += x;
            eval(k, l, r);
        }
        else{
            add(a, b, x, (k<<1)+1, l, (l+r)>>1);
            add(a, b, x, (k<<1)+2, (l+r)>>1, r);
            node[k] = std::min(node[(k<<1)+1], node[(k<<1)+2]);
        }
    }
    long long min(int a, int b, int k=0, int l=0, int r=-1){
        if(a >= b) return 9223372036854775807LL;
        if(r == -1) r = N;
        if(b <= l || r <= a) return 9223372036854775807LL;
        eval(k, l, r);
        if(a <= l && r <= b) return node[k];
        return std::min(min(a, b, (k<<1)+1, l, (l+r)>>1), min(a, b, (k<<1)+2, (l+r)>>1, r));
    }
    long long m;
    int find(int a, int b, int k=0, int l=0, int r=-1){
        if(r == -1){
            m = min(a, b);
            r = N;
        }
        if(r <= a || b <= l) return INT_MAX;
        if(r-l == 1){
            if(node[k] == m) return l;
            else return INT_MAX;
        }
        if(a <= l && r <= b){
            long long valueL = min(a, b, (k<<1)+1, l, (l+r)>>1);
            if(valueL == m) return find(a, b, (k<<1)+1, l, (l+r)>>1);
            long long valueR = min(a, b, (k<<1)+2, (l+r)>>1, r);
            if(valueR == m) return find(a, b, (k<<1)+2, (l+r)>>1, r);
            return INT_MAX;
        }
        return std::min(find(a, b, (k<<1)+1, l, (l+r)>>1), find(a, b, (k<<1)+2, (l+r)>>1, r));
    }
};

int N;
int p[200000];

RMinQ rmq0, rmq1;
priority_queue<pair<int, pair<int, int>>> pq;

signed main(){
    cin >> N;
    rmq0.init(N/2); rmq1.init(N/2);
    rep(i, N){
        cin >> p[i];
        if(i%2 == 0) rmq0.update(i/2, p[i]);
        else rmq1.update(i/2, p[i]);
    }

    pq.push({ -p[rmq0.find(0, N/2)*2],{0,N} });
    while(!pq.empty()){
        cout << -pq.top().first << " ";
        int l = pq.top().second.first;
        int r = pq.top().second.second;
        pq.pop();
        if(l%2 == 0){
            int tmp0 = rmq0.find(l/2, r/2);
            int tmp1 = rmq1.find(tmp0, r/2);
            cout << p[tmp1*2+1] << " ";
            if(l < tmp0*2){
                pq.push({ -p[rmq0.find(l/2, tmp0)*2], {l, tmp0*2} });
            }
            if(tmp0 < tmp1){
                pq.push({ -p[rmq1.find(tmp0, tmp1)*2+1], {tmp0*2+1, tmp1*2+1} });
            }
            if(tmp1*2+1+1 < r){
                pq.push({ -p[rmq0.find(tmp1+1, r/2)*2], {tmp1*2+1+1, r} });
            }
        }
        else{
            int tmp1 = rmq1.find(l/2, r/2);
            int tmp0 = rmq0.find(tmp1+1, r/2+1);
            cout << p[tmp0*2] << " ";
            if(l < tmp1*2+1){
                pq.push({ -p[rmq1.find(l/2, tmp1)*2+1], {l, tmp1*2+1} });
            }
            if(tmp1+1 < tmp0){
                pq.push({ -p[rmq0.find(tmp1+1, tmp0)*2], {tmp1*2+1+1, tmp0*2} });
            }
            if(tmp0*2+1 < r){
                pq.push({ -p[rmq1.find(tmp0, r/2)*2+1], {tmp0*2+1, r} });
            }
        }
    }
}

Measured memory_bytes: 13596