#[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();
    }
}

trait Monoid<T>: SemiGroup<T> {
    fn identity() -> T;
}
trait SemiGroup<T>: Magma<T> {}
trait Magma<T> {
    fn operation(lhs: T, rhs: T) -> T;
}
#[derive(Debug)]
struct SegmentTree<T, U> {
    len: usize,
    nodes: Vec<T>,
    phantom: std::marker::PhantomData<U>,
}
impl<T, U> SegmentTree<T, U>
where
U: Monoid<T>,
T: Copy + Clone + std::fmt::Debug,
{
#[allow(dead_code)]
    fn new(n: usize) -> Self {
        let mut len = 1;
        while len < n {
            len *= 2;
        }
        SegmentTree {
            len: len,
            nodes: vec![U::identity(); 2 * len - 1],
            phantom: Default::default(),
        }
    }
#[allow(dead_code)]
    fn from_vec(vec: &Vec<T>) -> Self {
        let mut len = 1;
        while len < vec.len() {
            len *= 2;
        }
        let mut nodes = vec![U::identity(); 2 * len - 1];
        for i in 0..vec.len() {
            nodes[i + len - 1] = vec[i];
        }
        for i in (0..len - 1).rev() {
            nodes[i] = U::operation(nodes[2 * i + 1], nodes[2 * i + 2]);
        }
        SegmentTree {
            len: len,
            nodes: nodes,
            phantom: Default::default(),
        }
    }
#[allow(dead_code)]
    fn update(&mut self, index: usize, val: T) {
        let mut index = index;
        index += self.len - 1;
        self.nodes[index] = val;
        while index > 0 {
            index = (index - 1) / 2;
            self.nodes[index] = U::operation(self.nodes[2 * index + 1], self.nodes[2 * index + 2]);
        }
    }
    fn get(&self, l: usize, r: usize) -> T {
        self._get(l, r, 0, 0, self.len)
    }
    fn _get(&self, ql: usize, qr: usize, index: usize, l: usize, r: usize) -> T {
        if r <= ql || qr <= l {
            return U::identity();
        }
        if ql <= l && r <= qr {
            return self.nodes[index];
        }
        let vl = self._get(ql, qr, 2 * index + 1, l, (l + r) / 2);
        let vr = self._get(ql, qr, 2 * index + 2, (l + r) / 2, r);
        U::operation(vl, vr)
    }
}

struct Calc {}

impl Magma<(usize, usize)> for Calc {
    fn operation(lhs: (usize, usize), rhs: (usize, usize)) -> (usize, usize) {
        min!(lhs, rhs)
    }
}

impl SemiGroup<(usize, usize)> for Calc {}

impl Monoid<(usize, usize)> for Calc {
    fn identity() -> (usize, usize) {
        (std::usize::MAX, 0)
    }
}


struct Range { //[l,r)
    minimum: usize,
    left: usize,
    right: usize,
}

impl Range {
    fn new(minimum: usize, left: usize, right: usize) -> Self {
        Range{
            minimum: minimum,
            left: left,
            right: right,
        }
    }
}

impl PartialOrd for Range {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(other.minimum.cmp(&self.minimum))
    }
}

impl PartialEq for Range {
    fn eq(&self, other: &Self) -> bool {
        self.minimum == other.minimum
    }
}

impl Eq for Range {}

impl Ord for Range {
    fn cmp(&self, other: &Self) -> Ordering {
        other.minimum.cmp(&self.minimum)
    }
}

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

    let mut ep = Vec::with_capacity(n);
    let mut op = Vec::with_capacity(n);
    for i in 0..n {
        if i % 2 == 0 {
            ep.push((p[i], i));
            op.push((std::usize::MAX,i));
        } else {
            op.push((p[i],i));
            ep.push((std::usize::MAX,i));
        }
    }
    let mut trees = Vec::with_capacity(2);
    trees.push(SegmentTree::<(usize, usize), Calc>::from_vec(&ep));
    trees.push(SegmentTree::<(usize, usize), Calc>::from_vec(&op));

    let mut res = Vec::with_capacity(n);
    let mut heap = BinaryHeap::new();
    heap.push(Range::new(trees[0].get(0,n).0, 0,n));
    while let Some(range) = heap.pop() {
        let left = range.left;
        let right = range.right;
        let (val, i) = trees[left%2].get(left,right);
        res.push(val);
        let (val, j) = trees[(left+1)%2].get(i+1, right);
        res.push(val);
        if left < i {
            heap.push(Range::new(trees[left%2].get(left, i).0,left,i));
        }
        if i+1 < j {
            heap.push(Range::new(trees[(i+1)%2].get(i+1, j).0,i+1,j));
        }
        if j+1 < right {
            heap.push(Range::new(trees[(j+1)%2].get(j+1, right).0,j+1,right));
        }
    }
    for e in res {
        print!("{} ", e);
    }
    println!("");

}


Measured memory_bytes: 40048