#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"

using namespace std;

const long long int MOD = 1000000007;

long long int N, M, K, H, W, L, R;

class Segment_Tree {
	vector<long long int>v;
	vector<int>l;
	vector<int>r;
	int num;
	long long int ret;
	bool is_min;
	long long int Update(int place) {
		if (place >= v.size() / 2) {
			return v[place];
		}
		if (is_min) {
			v[place] = min(Update(place * 2), Update(place * 2 + 1));
			return v[place];
		}
		v[place] = max(Update(place * 2), Update(place * 2 + 1));
		return v[place];
	}
	void RMQ(int a, int b, int place) {
		if (l[place] >= a && r[place] <= b) {
			if (is_min)ret = min(ret, v[place]);
			else ret = max(ret, v[place]);
			return;
		}
		if (l[place]>b || r[place]<a) return;
		RMQ(a, b, place * 2);
		RMQ(a, b, place * 2 + 1);
		return;
	}
public:
	void Left(int place) {
		if (place >= v.size() / 2) {
			l[place] = place - v.size() / 2;
			return;
		}
		Left(place * 2);
		Left(place * 2 + 1);
		l[place] = l[place * 2];
		return;
	}
	void Right(int place) {
		if (place >= v.size() / 2) {
			r[place] = place - v.size() / 2;
			return;
		}
		Right(place * 2);
		Right(place * 2 + 1);
		r[place] = r[place * 2 + 1];
		return;
	}
	Segment_Tree(int n, bool min) {
		n++;
		num = 1;
		while (num < n * 2) {
			num *= 2;
		}
		l.resize(num);
		r.resize(num);
		is_min = min;
		if (min) {
			v.resize(num, LLONG_MAX);
		}
		else v.resize(num, LLONG_MIN);
		Left(1);
		Right(1);
	}
	void Insert(int place, long long int num, bool update) {
		place += v.size() / 2;
		v[place] = num;
		if (!update)return;
		place /= 2;
		while (place) {
			if (is_min)v[place] = min(v[place * 2], v[place * 2 + 1]);
			else v[place] = max(v[place * 2], v[place * 2 + 1]);
			place /= 2;
		}
	}
	void TopDown() {
		Update(1);
	}
	long long int RMQ(int a, int b) {
		if (is_min)ret = LLONG_MAX;
		else ret = LLONG_MIN;
		RMQ(a, b, 1);
		return ret;
	}
};

struct range {
	long long int l;
	long long int r;
	long long int f;
	long long int s;
	bool operator <(const range& r)const {
		return f < r.f;
	}
	bool operator >(const range& r)const {
		return f > r.f;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>v(N);
	Segment_Tree odd(N, true);
	Segment_Tree even(N, true);
	for (int i = 0; i < N; i++) {
		cin >> v[i];
		if (i % 2)odd.Insert(i, MOD*v[i] + i, false);
		else even.Insert(i, MOD*v[i] + i, false);
	}
	odd.TopDown();
	even.TopDown();
	priority_queue<range, vector<range>, greater<range>>PQ;
	long long int box = even.RMQ(0, N - 1);
	PQ.push({ 0,N - 1,even.RMQ(0,N - 1),odd.RMQ(box%MOD,N - 1) });
	vector<int>ans;
	while (!PQ.empty()) {
		long long int l = PQ.top().l;
		long long int r = PQ.top().r;
		long long int f = PQ.top().f;
		long long int s = PQ.top().s;
	//	cout << l << " " << r << endl;
		PQ.pop();
		ans.push_back(f / MOD);
		ans.push_back(s / MOD);
		f %= MOD;
		s %= MOD;
		if (f - l > 1) {
			if (l % 2) {
				long long int box = odd.RMQ(l, f - 1);
			//	cout << box % MOD << endl;
				PQ.push({ l,f - 1,odd.RMQ(l,f - 1),even.RMQ(box%MOD,f - 1) });
			}
			else {
				long long int box = even.RMQ(l, f - 1);
			//	cout << box % MOD << endl;
				PQ.push({ l,f - 1,even.RMQ(l,f - 1),odd.RMQ(box%MOD,f - 1) });
			}
		}
		if (s - f > 2) {
			if ((f + 1) % 2) {
				long long int box = odd.RMQ(f + 1, s - 1);
			//	cout << box % MOD << endl;
				PQ.push({ f + 1,s - 1,odd.RMQ(f + 1,s - 1),even.RMQ(box%MOD,s - 1) });
			}
			else {
				long long int box = even.RMQ(f + 1, s - 1);
			//	cout << box % MOD << endl;
				PQ.push({ f + 1,s - 1,even.RMQ(f + 1,s - 1),odd.RMQ(box%MOD,s - 1) });
			}
		}
		if (r - s > 1) {
			if ((s + 1) % 2) {
				long long int box = odd.RMQ(s + 1, r);
			//	cout << box % MOD << endl;
				PQ.push({ s + 1,r,odd.RMQ(s + 1,r),even.RMQ(box%MOD,r) });
			}
			else {
				long long int box = even.RMQ(s + 1, r);
			//	cout << box % MOD << endl;
				PQ.push({ s + 1,r,even.RMQ(s + 1,r),odd.RMQ(box%MOD,r) });
			}
		}
	}
	for (auto i : ans)cout << i << " ";
	cout << endl;
	return 0;
}

Measured memory_bytes: 21744