Problem:
Score : 800 points

Problem Statement

Let N be a positive even number.

We have a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N).
Snuke is constructing another permutation of (1, 2, ..., N), q, following the procedure below.

First, let q be an empty sequence.
Then, perform the following operation until p becomes empty:

Select two adjacent elements in p, and call them x and y in order. Remove x and y from p (reducing the length of p by 2), and insert x and y, preserving the original order, at the beginning of q.

When p becomes empty, q will be a permutation of (1, 2, ..., N).

Find the lexicographically smallest permutation that can be obtained as q.

Constraints

N is an even number.

2 ≤ N ≤ 2 × 10^5

p is a permutation of (1, 2, ..., N).

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print the lexicographically smallest permutation, with spaces in between.

Sample Input 1

4
3 2 4 1

Sample Output 1

3 1 2 4

The solution above is obtained as follows:

p

q

(3, 2, 4, 1)

()

↓

↓

(3, 1)

(2, 4)

↓

↓

()

(3, 1, 2, 4)

Sample Input 2

2
1 2

Sample Output 2

1 2

Sample Input 3

8
4 6 3 2 8 5 7 1

Sample Output 3

3 1 2 7 4 6 8 5

The solution above is obtained as follows:

p

q

(4, 6, 3, 2, 8, 5, 7, 1)

()

↓

↓

(4, 6, 3, 2, 7, 1)

(8, 5)

↓

↓

(3, 2, 7, 1)

(4, 6, 8, 5)

↓

↓

(3, 1)

(2, 7, 4, 6, 8, 5)

↓

↓

()

(3, 1, 2, 7, 4, 6, 8, 5)

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>

#define rep(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define irep(i, a, b) for(int (i)=(a);(i)<=(b);(i)++)
#define rrep(i, a, b) for(int (i)=(a);(i)>=(b);(i)--)

using namespace std;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;


struct SegTree {
    SegTree(int sz) {
        original_n = sz;
        n = 1;
        while (n < original_n) n *= 2;
        treen = 2 * n - 1;
        tree.resize(treen, INF);
    }
    void update(int i, int x) {
        int now = n + i - 1;
        tree[now] = x;
        while (now > 0) {
            now = (now - 1) / 2;
            tree[now] = min(tree[now * 2 + 1], tree[now * 2 + 2]);
        }
    }
    int query(int a, int b) {
        return query(a, b, 0, n, 0);
    }
    int query(int a, int b, int l, int r, int k) {
        if (r <= a || b <= l) return INF;
        if (a <= l&&r <= b) return tree[k];
        int left = query(a, b, l, (l + r) / 2, k * 2 + 1);
        int right = query(a, b, (l + r) / 2, r, k * 2 + 2);

        return min(left, right);
    }
    int treen;
    int n;
    int original_n;
    vector<int> tree;
};


void merge(vector<int> &vec1, vector<int> &vec2, vector<int> &dst, int from) {
    int i = 0, j = 0;
    while (i < vec1.size() || j < vec2.size()) {
        if (i < vec1.size() && (j == vec2.size() || vec1[i] < vec2[i])) {
            dst[i + j + from] = vec1[i];
            i++;
        }
        else {
            dst[i + j + from] = vec2[j];
            j++;
        }
    }
}


vector<int> recur(int begin, int end, bool flip, vector<int> &p, vector<int> &pos, SegTree &odd, SegTree &even, set<int> &outputs) {
    if (end - begin == 0)
        return {};
    if (end - begin == 2) {
        return { 10 * p[begin] + p[begin + 1] };
    }

    SegTree &target = (flip ? odd : even);
    SegTree &after = (flip ? even : odd);

    int minx = target.query(begin / 2, end / 2);
    int posx = pos[minx];
    int minx2 = after.query((posx + 1) / 2, (end / 2));
    int posx2 = pos[minx2];


    auto res1 = recur(begin, posx, flip, p, pos, odd, even, outputs);
    auto res2 = recur(posx + 1, posx2, !flip, p, pos, odd, even, outputs);
    auto res3 = recur(posx2 + 1, end, flip, p, pos, odd, even, outputs);

    vector<int> merges(res1.size() + res2.size());
    merge(res1, res2, merges, 0);
    vector<int> merges2(merges.size() + res3.size());
    merge(merges, res3, merges2, 0);

    vector<int> ret;
    ret.resize(merges2.size() + 1);
    ret[0] = 10 * minx + minx2;
    copy(merges2.begin(), merges2.end(), ret.begin() + 1);

    return ret;
}


int main() {
    int n;
    cin >> n;
    vector<int> p(n);
    rep(i, 0, n)
        cin >> p[i];

    set<int> outputs;

    SegTree odd(n / 2), even(n / 2);
    rep(i, 0, n) {
        if (i % 2 == 0)
            even.update(i / 2, p[i]);
        else
            odd.update(i / 2, p[i]);
    }
    vector<int> pos(n + 1);
    rep(i, 0, n)
        pos[p[i]] = i;


    auto ret = recur(0, n, false, p, pos, odd, even, outputs);
    rep(i, 0, ret.size()) {
        cout << (i == 0 ? "" : " ") << ret[i] / 10 << " " << ret[i] % 10;
    }
    cout << endl;




    return 0;
}