// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T> inline bool chmin(T& x, const T& y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T> inline bool chmax(T& x, const T& y) {if (x < y) {x = y; return 1;} return 0;}
struct IOSET {IOSET() {cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10);}} ioset;

// Lazy Segment Tree
template <typename Operator>
class LazySegmentTree {
    Operator OP;
    using NodeType = decltype(OP.NodeE);
    using FuncType = decltype(OP.FuncE);
    int length, height;
    vector<NodeType> node;
    vector<FuncType> lazy;
    vector<int> width;
    void eval(int n) {
        node[n] = OP.NodeFunc(node[n], lazy[n], width[n]);
        if (n < length) {
            lazy[(n << 1) | 0] = OP.MergeFunc(lazy[(n << 1) | 0], lazy[n]);
            lazy[(n << 1) | 1] = OP.MergeFunc(lazy[(n << 1) | 1], lazy[n]);
        }
        lazy[n] = OP.FuncE;
    }
public:
    LazySegmentTree(int n) {
        length = 1;
        height = 0;
        while (length < n) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        lazy.resize(length << 1, OP.FuncE);
        width.resize(length << 1, 1);
        for (int i = length - 1; i > 0; --i) width[i] = width[i << 1] << 1;
    }
    LazySegmentTree(vector<NodeType> &vec) {
        length = 1;
        height = 0;
        while (length < vec.size()) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        rep(i, vec.size()) node[i + length] = vec[i];
        for (int i = length - 1; i > 0; --i) node[i] = OP.MergeNode(node[(i << 1) | 0], node[(i << 1) | 1]);
        lazy.resize(length << 1, OP.FuncE);
        width.resize(length << 1, 1);
        for (int i = length - 1; i > 0; --i) width[i] = width[i << 1] << 1;
    }
    void update(int a, int b, FuncType x) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i > 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        while (r > l) {
            if (l & 1) {
                lazy[l] = OP.MergeFunc(lazy[l], x);
                eval(l);
                ++l;
            }
            if (r & 1) {
                --r;
                lazy[r] = OP.MergeFunc(lazy[r], x);
                eval(r);
            }
            l >>= 1; r >>= 1;
        }
        l = a + length; r = b + length - 1;
        while (l >>= 1) node[l] = OP.MergeNode(OP.NodeFunc(node[(l << 1) | 0], lazy[(l << 1) | 0], width[(l << 1) | 0]), OP.NodeFunc(node[(l << 1) | 1], lazy[(l << 1) | 1], width[(l << 1) | 1]));
        while (r >>= 1) node[r] = OP.MergeNode(OP.NodeFunc(node[(r << 1) | 0], lazy[(r << 1) | 0], width[(r << 1) | 0]), OP.NodeFunc(node[(r << 1) | 1], lazy[(r << 1) | 1], width[(r << 1) | 1]));
    }
    NodeType get(int a, int b) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i >= 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        NodeType vl = OP.NodeE, vr = OP.NodeE;
        while (l < r) {
            if (l & 1) {
                vl = OP.MergeNode(vl, OP.NodeFunc(node[l], lazy[l], width[l]));
                ++l;
            }
            if (r & 1) {
                --r;
                vr = OP.MergeNode(OP.NodeFunc(node[r], lazy[r], width[r]), vr);
            }
            l >>= 1; r >>= 1;
        }
        return OP.MergeNode(vl, vr);
    }
};
struct Node {
    int even_val, odd_val;
};
struct RMQ {
    using NodeType = Node;
    using FuncType = int;
    NodeType NodeE = Node{inf, inf};
    FuncType FuncE = 0;
    NodeType MergeNode(NodeType x, NodeType y) {
        return Node{min(x.even_val, y.even_val), min(x.odd_val, y.odd_val)};
    }
    FuncType MergeFunc(FuncType x, FuncType y) {
        if (x == 2 || y == 2) return 2;
        else return x ^ y;
    }
    NodeType NodeFunc(NodeType x, FuncType y, int length) {
        if (y == 2) return Node{inf, inf};
        if (y == 1) return Node{x.odd_val, x.even_val};
        return x;
    }
};
struct Right {
    using NodeType = int;
    using FuncType = int;
    NodeType NodeE = inf;
    FuncType FuncE = inf;
    NodeType MergeNode(NodeType x, NodeType y) {
        return min(x, y);
    }
    FuncType MergeFunc(FuncType x, FuncType y) {
        return min(x, y);
    }
    NodeType NodeFunc(NodeType x, FuncType y, int length) {
        return min(x, y);
    }
};

// Main
int main() {
    int n;
    cin >> n;
    vector<int> p(n);
    rep(i, n) cin >> p[i];
    
    vector<Node> vec0(n);
    vector<int> vec1(n, n);
    rep(i, n) {
        if (i & 1) vec0[i] = Node{inf, p[i]};
        else vec0[i] = Node{p[i], inf};
    }
    LazySegmentTree<RMQ> st0(vec0);
    LazySegmentTree<Right> st1(vec1);
    
    
    vector<int> idx(n + 1, -1);
    rep(i, n) idx[p[i]] = i;
    
    vector<int> q;
    rep(i, n / 2) {
        auto x = st0.get(0, n);
        int l = idx[x.even_val];
        auto y = st0.get(l + 1, st1.get(l + 1, l + 2));
        int r = idx[y.odd_val];
        q.push_back(x.even_val);
        q.push_back(y.odd_val);
        st0.update(l, l + 1, 2);
        st0.update(r, r + 1, 2);
        st0.update(l + 1, r, 1);
        st1.update(0, l, l);
        st1.update(l + 1, r, r);
    }
    
    rep(i, n) cout << q[i] << " \n"[i == n - 1];
    return 0;
}


Measured memory_bytes: 22580