#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()


enum QUERY_GET { SUM, MAXIMUM, MINIMUM };
template <class T = int>
class SegmentTree {
private:
    int ARY_SIZE;
    T initVal;
    std::vector<T> ary;
    std::function<T(T&, T&)> merge;
 
    void init(int n, QUERY_GET qtype) {
        switch (qtype) {
        case SUM:
            initVal = 0;
            merge = [](T& l, T& r) {return l + r; };
            break;
        case MAXIMUM:
            initVal = std::numeric_limits<T>::lowest();
            merge = [](T& l, T& r) {return (l > r) ? l : r; };
            break;
        case MINIMUM:
            initVal = std::numeric_limits<T>::max();
            merge = [](T& l, T& r) {return (l < r) ? l : r; };
            break;
        default: struct INVALID_QUERY_TYPE_ERROR {}; throw INVALID_QUERY_TYPE_ERROR(); break;
        }
        init(n);
    }
    void init(int n) {
        while (ARY_SIZE < n) ARY_SIZE <<= 1;
        ary.resize(ARY_SIZE << 1, initVal);
    }
public:
    SegmentTree() {}
    SegmentTree(int n, QUERY_GET qtype) : ARY_SIZE(1) { init(n, qtype); }
    SegmentTree(int n, T initVal, std::function<T(T&, T&)> f) : ARY_SIZE(1), initVal(initVal), merge(f) { init(n); }
 
    inline void update(int i, T val) {
        i += ARY_SIZE;
        ary[i] = val;
        while (i > 1) {
            i >>= 1;
            ary[i] = merge(ary[i << 1], ary[(i << 1) + 1]);
        }
    }

    inline void add(int i, T val) {
        update(i, ary[i + ARY_SIZE] + val);
    }

    inline T query(int l, int r) {
        if (l >= r) return initVal;
        T vl = initVal, vr = initVal;
        for (l += ARY_SIZE, r += ARY_SIZE; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = merge(vl, ary[l++]);
            if (r & 1) vr = merge(ary[--r], vr);
        }
        return merge(vl, vr);
    }

    T operator[](int i) { return ary[i + ARY_SIZE]; }
 
    void debugShow() {
        for (int i = ARY_SIZE; i < ARY_SIZE << 1; ++i) std::cerr << ary[i] << " ";
        std::cerr << "\n";
    }
};

SegmentTree<int> steven, stodd;

const int MAX = 2e5+10;

int N;
int pos[MAX];

void solve() {
    vi ans;
    priority_queue<iii> q;
    q.push(MT(-steven.query(0, N), 0, N));
    while (!q.empty()) {
        iii t = q.top(); q.pop();
        int l = get<1>(t), r = get<2>(t);
        assert((r-l)%2 == 0);

        int a = -get<0>(t), p = pos[a];
        assert(l <= p && p < r);
        assert((p-l)%2 == 0);

        int b = (p%2) ? steven.query(p+1, r) : stodd.query(p+1, r);
        int p2 = pos[b];
        assert(p < p2 && p2 < r);
        assert((p2-p)%2 == 1);
        ans.PB(a); ans.PB(b);

        if (p2 - p > 1) {
            int c = ((p+1)%2) ? stodd.query(p+1, p2) : steven.query(p+1, p2);
            q.push(MT(-c, p+1, p2));
        }
        if (p - l > 0) {
            int c = (l%2) ? stodd.query(l, p) : steven.query(l, p);
            q.push(MT(-c, l, p));
        }
        if (r - p2 > 1) {
            int c = ((p2+1)%2) ? stodd.query(p2+1, r) : steven.query(p2+1, r);
            q.push(MT(-c, p2+1, r));
        }
    }

    cout << ans[0];
    FOR (i, 1, N) cout << " " << ans[i];
    cout << endl;
}

int main() {
    cin >> N;
    steven = SegmentTree<int>(N, MINIMUM);
    stodd = SegmentTree<int>(N, MINIMUM);
    REP (i, N) {
        int p; cin >> p;
        pos[p] = i;
        if (i%2) {
            stodd.update(i, p);
            steven.update(i, MAX);
        } else {
            steven.update(i, p);
            stodd.update(i, MAX);
        }
    }
    solve();
}

Measured memory_bytes: 9120