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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.io.InputStream
import java.util.PriorityQueue
import kotlin.comparisons.compareByDescending
import kotlin.comparisons.thenByDescending

private val input = FastScanner()

fun main(args: Array<String>) = input.run {
    val n = nextInt()
    val p = nextInts(n)

    val q = PriorityQueue<Pair<Node, Node>>(n,
            compareByDescending<Pair<Node, Node>> { Math.min(it.first.x, it.second.x) }
                    .thenByDescending { Math.max(it.first.x, it.second.x) })

    val nodes = Array(n) { Node(p[it]) }
    nodes.forEachIndexed { index, node ->
        if (index < nodes.lastIndex) {
            node.next = nodes[index + 1]
            q.add(node to node.next!!)
        }
        if (index > 0) {
            node.prev = nodes[index - 1]
        }
    }

    val removed = BooleanArray(n + 1)
    val ans = IntArray(n)
    var pos = n - 2
    while (q.isNotEmpty()) {
        val (a, b) = q.remove()
        if (!removed[a.x] && !removed[b.x]) {
            a.remove()
            b.remove()
            removed[a.x] = true
            removed[b.x] = true
            ans[pos] = a.x
            ans[pos + 1] = b.x
            pos -= 2
            if (a.prev != null && b.next != null) {
                q.add(a.prev!! to b.next!!)
            }
        }
    }
    println(ans.joinToString(" "))
}

class Node(val x: Int) {
    var prev: Node? = null
    var next: Node? = null

    fun remove() {
        prev?.next = next
        next?.prev = prev
    }
}

class FastScanner(private val input: InputStream = System.`in`) {
    private val sb = StringBuilder()
    private val buffer = ByteArray(4096)
    private var pos = 0
    private var size = 0

    fun nextLong(): Long {
        var c = skipWhitespace()

        val sign = if (c == '-'.toInt()) {
            c = read()
            -1
        } else 1

        var ans = 0L

        while (c > ' '.toInt()) {
            ans = ans * 10 + c - '0'.toInt()
            c = read()
        }

        return sign * ans
    }

    fun nextInt() = nextLong().toInt()

    fun nextInts(n: Int) = IntArray(n) { nextInt() }

    private fun skipWhitespace(): Int {
        while (true) {
            val c = read()
            if (c > ' '.toInt() || c < 0) return c
        }
    }

    private fun read(): Int {
        while (pos >= size) {
            if (size < 0) return -1
            size = input.read(buffer, 0, buffer.size)
            pos = 0
        }
        return buffer[pos++].toInt()
    }
}