#include <bits/stdc++.h>
#define ADD(a, b) a = (a + (ll)b) % mod
#define MUL(a, b) a = (a * (ll)b) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define rer(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> ppi;
typedef vector<ll> vi;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest) { 
	cout << arg << " "; Debug(rest...); }
template<class T>ostream& operator<< (ostream& out, const vector<T>& v) {
	out << "[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<< ", ";out<<v.back();}out << "]";return out;}
template<class S, class T>ostream& operator<< (ostream& out, const pair<S, T>& v) {
	out << "(" << v.first << ", " << v.second << ")";return out;}
const int MAX_N = 200010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

struct T {
	pi v;
	T(pi v = pi(inf, -1)) : v(v) {}
	T operator+(const T& t) {
		return T(min(v, t.v));
	}
};

struct segtree{
	int n; vector<T> seg;
	void init(int mx) {
		n = 1;
		while(n < mx) n *= 2;
		seg = vector<T>(n * 2);
	}
	segtree(int mx = 0) {
		init(mx);
	}
	void update(int i, T x) {
		i += n - 1;
		seg[i] = x;
		while(i > 0) {
			i = (i - 1) / 2;
			seg[i] = seg[i * 2 + 1] + seg[i * 2 + 2];
		}
	}
	T ga(int a, int b, int k, int l, int r) {
		if(b <= l || r <= a) return T();
		if(a <= l && r <= b) return seg[k];
		else {
			T lv = ga(a, b, k * 2 + 1, l, (l + r) / 2);
			T rv = ga(a, b, k * 2 + 2, (l + r) / 2, r);
			return lv + rv;
		}
	}
	//edit from here
	pi get(int a, int b) { return ga(a, b, 0, 0, n).v; } //[a, b)
};

int N;
int A[MAX_N];
vector<int> G[MAX_N * 3];
pi P[MAX_N * 3];
int E;
segtree os, es;


void dfs(int l, int r, int k, bool odd) {
	//debug(l, r, k);
	assert(k == E);
	int a = l / 2, b, c, d = r / 2;
	pi fp, sp;
	if(!odd) {
		fp = es.get(a, d); b = fp.sec;
		sp = os.get(b, d); c = sp.sec;
		if(b == -1 || c == -1) {
			P[k] = T().v;
			return;
		}

		es.update(b, T());
		os.update(c, T());
		a *= 2; d *= 2; b *= 2; c = c * 2 + 2;
	}
	else {
		fp = os.get(a, d); b = fp.sec;
		sp = es.get(b + 1, d); c = sp.sec;
		if(b == -1 || c == -1) {
			P[k] = T().v;
			return;
		}

		os.update(b, T());
		es.update(c, T());
		a *= 2; d *= 2; b = b * 2 + 2; c *= 2;
	}

	P[E] = pi(fp.fst, sp.fst);

	if(a != b) {
		E++; G[k].push_back(E);
		dfs(a, b, E, odd);
	}
	if(b != c) {
		E++; G[k].push_back(E);
		dfs(b, c, E, 1 ^ odd);
	}
	if(c != d) {
		E++; G[k].push_back(E);
		dfs(c, d, E, odd);
	}
}

pi ans[MAX_N];

void solve() {
	cin >> N;
	os.init(N / 2);
	es.init(N / 2);

	rep(i, 0, N) {
		cin >> A[i];
		if(i % 2 == 0) es.update(i / 2, pi(A[i], i / 2));
		else os.update(i / 2, pi(A[i], i / 2));
	}
	dfs(0, N, 0, false);
	//debug(vector<pi>(P, P + E + 1));
	int M = 0;
	priority_queue<ppi, vector<ppi>, greater<ppi> > que;
	que.push(make_pair(P[0], 0));
	while(!que.empty()) {
		ppi pp = que.top(); que.pop();
		//debug(M, pp.fst);
		ans[M++] = pp.fst;
		int v = pp.sec;
		rep(i, 0, (int)G[v].size()) {
			if(P[G[v][i]] != T().v) {
				que.push(make_pair(P[G[v][i]], G[v][i]));
			}
		}
	}
	rep(i, 0, M) {
		cout << ans[i].fst << " " << ans[i].sec << " ";
	}
	cout << "\n";
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(5);
#ifdef LOCAL
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}






Measured memory_bytes: 55168