#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
template<typename T, typename E>
struct SegmentTree {
    using F = function<T(T, T)>;
    using G = function<T(T, E)>;
    using H = function<E(E, E)>;
    using P = function<E(E, size_t)>;
    int n;
    F f; //要素と要素のマージ
    G g; //要素に作用素を作用
    H h; //作用素と作用素をマージ
    T ti; //要素の単位元?
    E ei; //作用素の単位元?
    P p; //b個の作用素aをマージした場合
    vector<T> dat;
    vector<E> laz;

    SegmentTree(int n_, F f, G g, H h, T ti, E ei,
                P p = [](E a, size_t b) {
                    b++;
                    return a;
                }) :
            f(f), g(g), h(h), ti(ti), ei(ei), p(p) {
        init(n_);
    }

    void init(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat.assign(2 * n - 1, ti);
        laz.assign(2 * n - 1, ei);
    }

    void build(int n_, vector<T> v) {
        for (int i = 0; i < n_; i++) dat[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--)
            dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
    }

    inline void eval(int len, int k) {
        if (laz[k] == ei) return;
        if (k * 2 + 1 < n * 2 - 1) {
            laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]);
            laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]);
        }
        dat[k] = g(dat[k], p(laz[k], len));
        laz[k] = ei;
    }

    T update(int a, int b, E x, int k, int l, int r) {
        eval(r - l, k);
        if (r <= a || b <= l) return dat[k];
        if (a <= l && r <= b) {
            laz[k] = h(laz[k], x);
            return g(dat[k], p(laz[k], r - l));
        }
        return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                          update(a, b, x, k * 2 + 2, (l + r) / 2, r));
    }

    T update(int a, int b, E x) {
        return update(a, b, x, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r) {
        eval(r - l, k);
        if (r <= a || b <= l) return ti;
        if (a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return f(vl, vr);
    }

    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }

    T query(int a) {
        return query(a, a + 1, 0, 0, n);
    }

    /**
     * kをxに更新する
     */
    void update(int k, T x) {
        update(k, k + 1, x);
//        query(k,k+1);//evaluate
//        k+=n-1;
//        dat[k]=x;
//        while(k){
//            k=(k-1)/2;
//            dat[k]=f(dat[k*2+1],dat[k*2+2]);
//        }
    }
};


class Solve {
public:
    Int N;
    vector<Int> P;
    vector<Int> iP;

    auto make_indexTree() {
        using T = pair<int, int>; // value, time
        using E = pair<int, int>;
        auto f = [](T a, T b) {
            if (a.second > b.second) return a;
            else return b;
        }; // 要素と要素のマージ
        auto g = [](T a, E b) {
            if (a.second > b.second) return a;
            else return b;
        }; // 要素に作用素を作用
        auto h = [](E a, E b) {
            if (a.second > b.second) return a;
            else return b;
        }; // 作用素と作用素をマージ
        auto p = [](E a, int b) {
            return a;
        }; // 作用素を区間加算する場合
        T ti = make_pair(0, -1); //要素の単位元?
        E ei = make_pair(0, -1); //作用素の単位元?
        SegmentTree<T, E> treeone(N + 1, f, g, h, ti, ei, p);

        return treeone;
    }

    auto make_minTree() {
        using T = int;
        using E = int;
        auto f = [](T a, T b) { return min(a, b); }; // 要素と要素のマージ
        auto g = [](T a, E b) { return min(a, b); }; // 要素に作用素を作用
        auto h = [](E a, E b) { return min(a, b); }; // 作用素と作用素をマージ
        auto p = [](E a, int b) { return a; }; // 作用素を区間加算する場合
        T ti = INF; //要素の単位元?
        E ei = INF; //作用素の単位元?
        SegmentTree<T, E> treeone(N + 1, f, g, h, ti, ei, p);

        return treeone;
    }

    auto make_cntTree() {
        using T = int;
        using E = int;
        auto f = [](T a, T b) { return a + b; }; // 要素と要素のマージ
        auto g = [](T a, E b) { return a + b; }; // 要素に作用素を作用
        auto h = [](E a, E b) { return a + b; }; // 作用素と作用素をマージ
        auto p = [](E a, int b) { return a * b; }; // 作用素を区間加算する場合
        T ti = 0; //要素の単位元?
        E ei = 0; //作用素の単位元?
        SegmentTree<T, E> treeone(N + 1, f, g, h, ti, ei, p);

        return treeone;
    }

    void input() {
        cin >> N;
        P.resize(N);
        iP.resize(N);
        for (int i = 0; i < N; ++i) {
            cin >> P[i];
            P[i]--;
            iP[P[i]] = i;
        }
    }

    void input(int N_, const vector<int> &vec) {
        N = N_;
        P.resize(N);
        iP.resize(N);
        for (int i = 0; i < N; ++i) {
//            cin >> P[i];
            P[i] = vec[i];
            P[i]--;
            iP[P[i]] = i;
        }
    }


    void solve() {


        auto prevTree = make_indexTree();
        auto nextTree = make_indexTree();
        prevTree.update(0, N, make_pair(0, 0));
        nextTree.update(0, N, make_pair(N, 0));
        auto oddValue = make_minTree();
        auto evenValue = make_minTree();
        for (int i = 0; i < N; ++i) {
            if (i % 2 == 0) evenValue.update(i, i + 1, P[i]);
            if (i % 2 == 1) oddValue.update(i, i + 1, P[i]);
        }
        auto cntValue = make_cntTree();

        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq; //value
        vector<bool> used(N, false);
        vector<Int> ans;

        if (true) {
            Int lvalue = evenValue.query(0, N);
            Int rvalue = oddValue.query(iP[lvalue] + 1, N);
            pq.emplace(lvalue, rvalue);
        }

        int time = 1;
        for (int i = 0; i < N / 2; ++i) {
            auto top = pq.top();
            pq.pop();
            ans.push_back(top.first);
            ans.push_back(top.second);
            used[iP[top.first]] = used[iP[top.second]] = true;

            vector<Int> begs;
            if (iP[top.second] - iP[top.first] > 1) {
                if (!used[iP[top.first] + 1]) {
                    begs.push_back(iP[top.first] + 1);
                }
            }

            // prevTreeの更新
            prevTree.update(iP[top.first], iP[top.second] + 1, make_pair(iP[top.first] + 1, time++));
            if (iP[top.second] + 1 < N) {
                if (!used[iP[top.second] + 1]) {
                    prevTree.update(iP[top.second] + 1, nextTree.query(iP[top.second] + 1).first,
                                    make_pair(iP[top.second] + 1, time++));
                    begs.push_back(iP[top.second] + 1);
                }
            }


            // nextTreeの更新
            if (iP[top.first] - 1 >= 0) {
                if (!used[iP[top.first] - 1]) {
                    nextTree.update(prevTree.query(iP[top.first] - 1).first, iP[top.first],
                                    make_pair(iP[top.first], time++));
                    begs.push_back(iP[top.first] - 1);
                }
            }
            nextTree.update(iP[top.first] + 1, iP[top.second], make_pair(iP[top.second], time++));

            // cntValueの更新
            cntValue.update(iP[top.first], iP[top.second] + 1, 1);


            for (auto e : begs) {
                Int ldx = prevTree.query(e).first;
                Int rdx = nextTree.query(e).first;
                if (rdx - ldx < 2) continue;

                if (cntValue.query(ldx) % 2 == 0) {
                    Int lvalue = evenValue.query(ldx, rdx);
                    Int rvalue = oddValue.query(iP[lvalue] + 1, rdx);
                    pq.emplace(lvalue, rvalue);
                } else {
                    Int lvalue = oddValue.query(ldx, rdx);
                    Int rvalue = evenValue.query(iP[lvalue] + 1, rdx);
                    pq.emplace(lvalue, rvalue);
                }
            }
        }

        for (auto e : ans) {
            cout << e + 1 << " ";
        }
        cout << endl;
        return;


    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    bool debug = true;
    bool debug = false;

    if (!debug) {
        Solve solve;
        solve.input();
        solve.solve();
    } else {

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
        while (true) {
            int sz = 10;
            Int N = (rand() % (sz / 2) + 1) * 2;
            vector<int> vec;
            for (int i = 0; i < N; ++i) vec.push_back(i + 1);
            for (int i = 0; i < N; ++i) {
                int a = rand() % N;
                int b = rand() % N;
                swap(vec[a], vec[b]);
            }

            cout << "N = " << N << endl;
            cout << "P = ";
            for (auto e : vec) cout << e << " ";
            cout << endl;

            Solve solve;
            solve.input(N, vec);
            solve.solve();

            cout << "----------------" << endl;
        }
#pragma clang diagnostic pop

    }

    return 0;
}


Measured memory_bytes: 35440