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 Rust (1.15.1) (judge verdict: Runtime Error)

#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

fn dfs(vec: &[usize], k: usize) -> Vec<usize> {
    if k == 1 {
        return vec.to_vec();
    }
    let mut mnf = Vec::new();
    let mut mnb = Vec::new();
    for i in 1..k {
        let l = dfs(&vec[0..2*i], i);
        let r = dfs(&vec[2*i..2*k], k-i);
        if l[0] < r[0] {
            if mnf.len() == 0 || mnf[0] > l[0] || (mnf[0] == l[0] && mnf[1] > l[1]) {
                mnf = l;
                mnb = r;
            }
        } else {
            if mnf.len() == 0 || mnf[0] > r[0] || (mnf[0] == r[0] && mnf[1] > r[1]) {
                mnf = r;
                mnb = l;
            }
        }
    }
    let mut l = dfs(&[vec[0], vec[2*k-1]], 1);
    let mut r = dfs(&vec[1..2*k-1], k-1);
    if mnf[0] > l[0] || (mnf[0] == l[0] && mnf[1] > l[1]) {
        l.append(&mut r);
        return l;
    }
    merge(mnf, mnb)
}

fn merge(vec1: Vec<usize>, vec2: Vec<usize>) ->  Vec<usize> {
    let mut i = 0;
    let mut j = 0;
    let len1 = vec1.len()/2;
    let len2 = vec2.len()/2;
    let mut vec = vec![0; vec1.len()+vec2.len()];
    while i < len1 || j < len2 {
        if j >= len2 || (i < len1 && vec1[2*i] < vec2[2*j]) {
            vec[2*(i+j)] = vec1[2*i];
            vec[2*(i+j)+1] = vec1[2*i+1];
            i += 1;
        } else {
            vec[2*(i+j)] = vec2[2*j];
            vec[2*(i+j)+1] = vec2[2*j+1];
            j += 1;
        }
    }
    vec
}

fn solve() {
    let n = get!(usize);
    let p = get!([usize]);

    let res = dfs(p.as_slice(), n/2);
    for e in res {
        print!("{} ", e);
    }
    println!("");
}