#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <cmath>
#include <unordered_map>
#include <cstdio>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long ll;

ll gcd(ll x, ll y) {
	if (y == 0)return x;
	return gcd(y, x%y);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y)*y;
}
ll kai(ll x, ll y, ll m) {
	ll res = 1;
	for (ll i = x - y + 1; i <= x; i++) {
		res *= i; res %= m;
	}
	return res;
}
ll mod_pow(ll x, ll y, ll m) {
	ll res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}

ll comb(ll x, ll y, ll m) {
	if (y > x)return 0;
	return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}

const int INF = 1000000000;

int n, a[200010];

int s = 1;
vector<int> dat, dat2;

void init() {
	while (s < n)s *= 2;
	dat.resize(2 * s - 1);
	dat2.resize(2 * s - 1);
	for (int i = 0; i < n; i++) {
		if (i % 2)dat[s - 1 + i] = a[i], dat2[s - 1 + i] = INF;
		else dat2[s - 1 + i] = a[i], dat[s - 1 + i] = INF;
	}
	for (int i = s - 1 + n; i <= 2 * s - 2; i++)dat[i] = dat2[i] = INF;
	for (int i = s - 2; i >= 0; i--)dat[i] = min(dat[2 * i + 1], dat[2 * i + 2]), dat2[i] = min(dat2[2 * i + 1], dat2[2 * i + 2]);
}

int query(int a, int b, int k = 0, int l = 0, int r = s) {
	if (r <= a || b <= l)return INF;
	if (a <= l && r <= b)return dat[k];
	int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
	int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
	return min(vl, vr);
}

int query2(int a, int b, int k = 0, int l = 0, int r = s) {
	if (r <= a || b <= l)return INF;
	if (a <= l && r <= b)return dat2[k];
	int vl = query2(a, b, 2 * k + 1, l, (l + r) / 2);
	int vr = query2(a, b, 2 * k + 2, (l + r) / 2, r);
	return min(vl, vr);
}

int p[200010];
priority_queue<pair<int, pair<int, int>>, vector< pair<int, pair<int, int>>>, greater< pair<int, pair<int, int>>>> que;
vector<int> ans;

signed main() {
	cin >> n;
	for (int i = 0; i < n; i++)cin >> a[i], p[a[i]] = i;
	init();
	que.push(make_pair(query2(0, n), make_pair(0, n)));
	for (int i = 0; i < n / 2; i++) {
		pair<int, pair<int, int>> q = que.top(); que.pop();
		ans.push_back(q.first);
		int x = 0;
		if (q.second.first % 2)x = query2(p[q.first] + 1, q.second.second);
		else x = query(p[q.first] + 1, q.second.second);
		ans.push_back(x);
		if (q.second.first != p[q.first]) {
			if (q.second.first % 2)que.push(make_pair(query(q.second.first, p[q.first]), make_pair(q.second.first, p[q.first])));
			else que.push(make_pair(query2(q.second.first, p[q.first]), make_pair(q.second.first, p[q.first])));
		}
		if (p[q.first] + 1 != p[x]) {
			if (p[q.first] % 2)que.push(make_pair(query2(p[q.first] + 1, p[x]), make_pair(p[q.first] + 1, p[x])));
			else que.push(make_pair(query(p[q.first] + 1, p[x]), make_pair(p[q.first] + 1, p[x])));
		}
		if (p[x] != q.second.second) {
			if (p[x] % 2)que.push(make_pair(query2(p[x] + 1, q.second.second), make_pair(p[x] + 1, q.second.second)));
			else que.push(make_pair(query(p[x] + 1, q.second.second), make_pair(p[x] + 1, q.second.second)));
		}
	}
	for (int i = 0; i < n; i++)cout << ans[i] << " ";
	cout << endl;
}




Measured memory_bytes: 11328