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 Ruby (2.3.3) (judge verdict: Runtime Error)

class SegTreeMin
    attr_reader :dat
    def initialize(n_input, top=Float::INFINITY)
        @n_input = n_input
        @n = 1 << @n_input.to_s(2).length
        @top = top
        @dat = Array.new(2 * @n - 1, @top)
    end

    def update(k, a)
        k += @n - 1
        @dat[k] = a
        while k > 0 do
            k = (k - 1) / 2
            #@dat[k] = [@dat[2*k+1], @dat[2*k+2]].min
            @dat[k] = @dat[2*k+1]
            @dat[k] = @dat[2*k+2] if @dat[k] > @dat[2*k+2]
        end
    end

    # This works only when inserting larger value than all existing values
    def update_larger(k, a)
        k += @n - 1
        @dat[k] = a
        while k > 0 do
            k = (k - 1) / 2
            if @dat[k] <= a
                break
            else
                @dat[k] = a
            end
        end
    end

    # include a and b
    def min_between(a, b)
        query(a, b + 1, 0, 0, @n)
    end

    def query(a, b, k, l, r)
        return @top if r <= a || b <= l
        return @dat[k] if a <= l && r <= b
        vl = query(a, b, k * 2 + 1, l, (l + r) / 2)
        vr = query(a, b, k * 2 + 2, (l + r) / 2, r)
        #return [vl, vr].min
        return vl < vr ? vl : vr
    end
end


N = gets.to_i
ps = gets.split.map(&:to_i)

BIG = 2*N
$st = []
$st[0] = SegTreeMin.new(N, BIG)
$st[1] = SegTreeMin.new(N, BIG)
$pos = []
N.times do |i|
    $st[i%2].update(i, ps[i])
    $pos[ps[i]] = i
end

def rec(range_l, range_r)
    parity = range_l % 2
    left = $st[parity].min_between(range_l, range_r)
    pl = $pos[left]
    $st[parity].update(pl, BIG)
    right = $st[1-parity].min_between(pl, range_r)
    pr = $pos[right]
    $st[1-parity].update(pr, BIG)
    ret = [left, right]
    arrs = []
    if range_l < pl
        arrs.push(rec(range_l, pl - 1))
    end
    if pl + 1 < pr
        arrs.push(rec(pl + 1, pr - 1))
    end
    if pr < range_r
        arrs.push(rec(pr + 1, range_r))
    end
    while arrs.length > 0
        index = arrs.length.times.min_by{|i| arrs[i]}
        ret.concat(arrs[index].shift(2))
        arrs.delete_at(index) if arrs[index].length == 0
    end
    ret
end

puts rec(0, N-1).join(' ')