#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int op(int a,int b){ return min(a, b); } int e(){ return 998244353; } int main(){ int n; cin >> n; vector p(n), q(n); rep(i,0,n){ cin >> p[i]; p[i]--; q[p[i]] = i; } segtree seg1(n / 2); segtree seg2(n / 2); rep(i,0,n){ if (i % 2 == 0){ seg1.set(i / 2, p[i]); }else{ seg2.set(i / 2, p[i]); } } map> kukan; map, int> kug; int cnt = 0; priority_queue, int>> pq; auto calc = [&](pair t) -> pair { if (t.first % 2 == 0){ int x = seg1.prod(t.first / 2, t.second / 2); int y = seg2.prod(q[x] / 2, t.second / 2); return pair(x, y); }else{ int x = seg2.prod(t.first / 2, t.second / 2); int y = seg1.prod(q[x] / 2 + 1, t.second / 2 + 1); return pair(x, y); } }; auto hant = [&](pair a) -> pair { return pair(-a.first, -a.second); }; auto pqpush = [&](pair t) -> void { kukan[cnt] = t; kug[t] = cnt; pq.push(pair(hant(calc(t)), cnt)); cnt++; }; pqpush(pair(0, n)); vector ans; while(!pq.empty()){ auto k = pq.top(); pair tmp = hant(k.first); pq.pop(); ans.push_back(tmp.first); ans.push_back(tmp.second); pair h = kukan[k.second]; int a = q[tmp.first]; int b = q[tmp.second]; if (h.first < a){ pqpush(pair(h.first, a)); } if (b + 1 < h.second){ pqpush(pair(b + 1, h.second)); } if (a + 1 < b){ pqpush(pair(a + 1, b)); } } rep(i,0,n){ cout << ans[i] + 1; if (i == n-1) cout << '\n'; else cout << ' ' ; } }