#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
 
#define INF_LL (ll)1e18
#define INF (int)1e9
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
using ll = long long;
using PII = pair<int, int>;
 
const double eps = 1e-10;
 
template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}
 
class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;
 
public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}
 
	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}
 
	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;
 
		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}
 
	bool same(int x, int y){
		return find(x) == find(y);
	}
};
 
class LazySegTree{
private:
	int n;
	vector<pair<ll, ll> > node, node2[2];
	vector<bool> lazyFlag;
public:
	LazySegTree(){}
	LazySegTree(vector<ll> v){
		int sz = v.size();
		n = 1; while(n < sz) n *= 2;
		node.resize(2*n-1, {INF_LL, INF_LL});
		REP(i, 2)
			node2[i].resize(2*n-1, {INF_LL, INF_LL});
		lazyFlag.resize(2*n-1, false);
		REP(i, sz) node[i+n-1] = {v[i], i};
		REP(i, 2)
			REP(j, sz){
				if(j % 2 == i){
					node2[i][j+n-1] = {v[j], j};
				}else{
					node2[i][j+n-1] = {INF_LL, INF_LL};
				}
			}
		for(int i = n-2;i >= 0;i--) node[i] = min(node[2*i+1],node[2*i+2]);
		REP(j, 2)
			for(int i = n-2;i >= 0;i--) node2[j][i] = min(node2[j][2*i+1],node2[j][2*i+2]);
		
	}
 
 
	/*
	void add(int a, int b, ll x, int k=0, int l=0, int r=-1){
		if(r < 0) r = n;
		eval(k, l, r);
		if(b <= l || r <= a) return;
 
		if(a <= l && r <= b){
			lazy[k] += x;
			lazyFlag[k] = true;
			eval(k, l, r);
		}else{
			add(a, b, x, k*2+1, l, (l+r)/2);
			add(a, b, x, k*2+2, (l+r)/2, r);
			node[k] = node[k*2+1] + node[k*2+2];
		}
	}
	*/
 
	void update(int k, ll x){
		ll v = k+n-1;
		node[v] = {x, k};
		node2[0][v] = {x, k};
		node2[1][v] = {x, k};
		while(v > 0){
			v = (v-1)/2;
			node[v] = min(node[v*2+1], node[v*2+2]);
			node2[0][v] = min(node2[0][v*2+1], node2[0][v*2+2]);
			node2[1][v] = min(node2[1][v*2+1], node2[1][v*2+2]);
		}
	}
 
	pair<ll, ll> query(int a, int b, int k=0, int l=0, int r=-1){
		if(r < 0) r = n;
 
		if(b <= l || r <= a) return {INF_LL, INF_LL};
		if(a <= l && r <= b) return node[k];
 
		return min(query(a, b, k*2+1, l, (l+r)/2), query(a, b, k*2+2, (l+r)/2, r));
	}
 
	pair<ll, ll> query2(int a, int b, int type, int k = 0, int l = 0, int r = -1){
		if(r < 0) r = n;
		if(b <= l || r <= a) return {INF_LL, INF_LL};
		if(a <= l && r <= b) return node2[type][k];
 
		return min(query2(a, b, type, k*2+1, l, (l+r)/2), query2(a, b, type, k*2+2, (l+r)/2, r));
	}
};
 
using PLL = pair<ll, ll>;
 
 
vector<pair<int, PLL> > res;
int num;
LazySegTree sg;
vector<int> G[114514];
PII val[114514];
int cur = 0;
vector<ll> p;

struct data{
	PII v;
	int k;
	data(PII a, int b):v(a),k(b){}
};

bool operator<(const data &a, const data &b){
	return a.v > b.v;
}
 
int dfs(int l, int r){
	int px, py;
	px = sg.query2(l, r, l%2).second;
	py = sg.query2(px+1, r, (l+1)%2).second;

	val[cur] = {p[px], p[py]};
	int u = cur;
	cur++;
	if(px != l){
		G[u].push_back(cur);
		dfs(l, px);
	}

	if(py != px+1){
		G[u].push_back(cur);
		dfs(px+1, py);
	}

	if(py != r-1){
		G[u].push_back(cur);
		dfs(py+1, r);
	}
}
 
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
 
	int N;
 
	bool used[214514] = {};
 
	cin >> N;
	num = N;
	REP(i, N){
		int a;
		cin >> a;
		p.push_back(a);
	}
	sg = LazySegTree(p);
 
	dfs(0, N);
	priority_queue<data> que;	
	que.push(data(val[0], 0));
	vector<int> res;
	while(que.size()){
		PII p = que.top().v;
		int k = que.top().k;
		que.pop();

		res.push_back(p.first); res.push_back(p.second);
		REP(i, G[k].size()){
			que.push(data(val[G[k][i]], G[k][i]));
		}
	}
	REP(i, res.size()){
		cout << res[i] << " ";
	}
	cout << endl;
}


Measured memory_bytes: 58068