use std::io::*;
use std::str::FromStr;
use std::collections::BinaryHeap;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[allow(dead_code)]
#[derive(Clone, Ord, Eq, PartialOrd, PartialEq)]
struct Node {
    a: i32,
    index: usize,
}

#[allow(dead_code)]
impl Node {
    fn new(a: i32, index: usize) -> Node {
        Node { a: a, index: index }
    }
    fn default_value() -> Node {
        Node {
            a: i32::max_value(),
            index: 0,
        }
    }
}
#[allow(dead_code)]
fn merge(left: &Node, right: &Node) -> Node {
    std::cmp::min(left.clone(), right.clone())
}

#[allow(dead_code)]
#[derive(Clone)]
struct SegmentTree {
    max_depth: usize,
    data: Vec<Node>,
}
#[allow(dead_code)]
impl SegmentTree {
    fn new(n: usize) -> SegmentTree {
        let mut max_depth = 0;
        while (1 << max_depth) < n {
            max_depth += 1;
        }
        SegmentTree {
            max_depth: max_depth,
            data: vec![Node::default_value(); 1 << (max_depth + 1)],
        }
    }
    fn set(&mut self, index: usize, value: Node) {
        let mut target = (1 << self.max_depth) + index;
        self.data[target] = value;
        for _ in 0..self.max_depth {
            target >>= 1;
            self.data[target] = merge(&self.data[target * 2], &self.data[target * 2 + 1]);
        }
    }
    // [left, right)
    fn get(&self, left: usize, right: usize) -> Node {
        assert!(left < right);
        self.in_get(0, 1, left, right)
    }
    fn in_get(&self, depth: usize, node: usize, left: usize, right: usize) -> Node {
        let width = 1 << (self.max_depth - depth);
        let index = node - (1 << depth);
        let node_left = index * width;
        let node_mid = node_left + (width >> 1);
        if right - left == width && left == node_left {
            return self.data[node].clone();
        } else if right <= node_mid {
            return self.in_get(depth + 1, node * 2, left, right);
        } else if node_mid <= left {
            return self.in_get(depth + 1, node * 2 + 1, left, right);
        }
        merge(
            &self.in_get(depth + 1, node * 2, left, node_mid),
            &self.in_get(depth + 1, node * 2 + 1, node_mid, right),
        )
    }
}

fn main() {
    let n = read::<usize>();
    let mut seq = vec![0; n];
    let mut stree = vec![SegmentTree::new(n); 2];
    for i in 0..n {
        seq[i] = read::<i32>() - 1;
        stree[i % 2].set(i, Node::new(seq[i], i));
    }
    let mut que = BinaryHeap::new();
    let mut ans = vec![];
    que.push((-stree[0].get(0, n).a, 0, n));
    while let Some((_, l, r)) = que.pop() {
        let left = stree[l % 2].get(l, r);
        let left_value = left.a;
        let left_index = left.index;
        let right = stree[(left_index + 1) % 2].get(left_index + 1, r);
        let right_value = right.a;
        let right_index = right.index;
        ans.push(left_value);
        ans.push(right_value);
        if l != left_index {
            que.push((-stree[l % 2].get(l, left_index).a, l, left_index));
        }
        if left_index + 1 != right_index {
            que.push((
                -stree[(left_index + 1) % 2]
                    .get(left_index + 1, right_index)
                    .a,
                left_index + 1,
                right_index,
            ));
        }
        if right_index + 1 != r {
            que.push((
                -stree[(right_index + 1) % 2].get(right_index + 1, r).a,
                right_index + 1,
                r,
            ));
        }
    }
    for i in 0..n {
        if i != 0 {
            print!(" ");
        }
        print!("{}", ans[i] + 1);
    }
    println!("");
}


Measured memory_bytes: 26108