/* ▄▌▐▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▌ ▄▄██▌█ 宅急便です! ▄▄▄▌▐██▌█ Rating +25 :) をお届けに参りました! ███████▌█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▌ ▀(⊙)▀▀▀▀(⊙)(⊙)▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀(⊙ */ use std::cmp::Reverse; use std::collections::{BinaryHeap, BTreeSet}; use proconio::input; static INF: u64 = 1e18 as u64; trait ChLibs { fn chmin(&mut self, elm: T) -> bool; fn chmax(&mut self, elm: T) -> bool; } impl ChLibs for T { fn chmin(&mut self, elm: T) -> bool { return if *self > elm { *self = elm; true } else { false } } fn chmax(&mut self, elm: T) -> bool { return if *self < elm { *self = elm; true } else { false } } } fn solve(){ input! { n: usize, p: [u64; n] } let mut pq = BinaryHeap::new(); let mut indexes = (0..n).collect::>(); for (idx, &x) in p.iter().enumerate() { if idx == n-1 { continue; } pq.push(Reverse((x, idx))); } let mut seen = vec![false; n]; let mut ans = vec![]; while let Some(Reverse((x, idx))) = pq.pop() { if seen[idx] { continue; } seen[idx] = true; seen[indexes[idx+1]] = true; ans.push(x); ans.push(p[indexes[idx+1]]); if idx != n-2 { indexes[idx+1] = indexes[idx+2]; indexes[idx] = indexes[idx+1]; } } for (idx, &x) in ans.iter().enumerate() { print!("{}{}", x, if idx == ans.len()-1 { "" } else { " " }); } println!(); } fn main() { // input! { i: usize } let mut i = 1; while i != 0 { solve(); i -= 1; } }