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: Wrong Answer)

#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rev(a) reverse(all(a))
#define sort(a) sort(all(a));
#define nod(a, b) __gcd(a, b)

using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int a[n];
    int m[n + 1];
    bool was[n + 1];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        m[a[i]] = i;
        was[a[i]] = 0;
    }
    set<pair<int, int> > s;
    for (int i = 0; i < n - 1; i++)
    {
        s.insert({min(a[i], a[i + 1]), max(a[i], a[i + 1])});
    }
    vector<int> ans;
    while (!s.empty())
    {
        pair<int, int> p = *(--s.end());
        s.erase(p);
        was[p.first] = 1;
        was[p.second] = 1;
        if (m[p.first] > m[p.second])
        {
            if (m[p.first] != n - 1) s.erase({min(p.first, a[m[p.first] + 1]), max(p.first, a[m[p.first] + 1])});
            if (m[p.second] != 0) s.erase({min(p.second, a[m[p.second] - 1]), max(p.second, a[m[p.second] - 1])});
            ans.pb(p.first);
            ans.pb(p.second);
        }
        else
        {
            if (m[p.second] != n - 1) s.erase({min(p.second, a[m[p.second] + 1]), max(p.second, a[m[p.second] + 1])});
            if (m[p.first] != 0) s.erase({min(p.first, a[m[p.first] - 1]), max(p.first, a[m[p.first] - 1])});
            ans.pb(p.second);
            ans.pb(p.first);
        }
        if (min(m[p.first], m[p.second]) == 0 || max(m[p.first], m[p.second]) == n - 1)
        {

        }
        else
        {
            int x = a[min(m[p.first], m[p.second]) - 1], y = a[max(m[p.first], m[p.second]) + 1];
            if (was[x] == 0 && was[y] == 0) s.insert({min(x, y), max(x, y)});
        }
    }
    rev(ans);
    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i] << " ";
    }
    return 0;
}
/*
8
8 7 5 4 3 2 1 6
*/