#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define int long long
#define pb push_back
#define fi first
#define se second
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pii pair<int,int>
#define sz(x) (int)x.size()
const int mod = 1e9 + 7;
const int mod1 = 998244353;
typedef long double f80;

#ifndef LOCAL
#pragma GCC optimize ("O2")
#define endl '\n'
#endif

template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r){
    uniform_int_distribution<int> uid(l, r);
    return uid(rng);
}

ll pwr(ll a,ll b, ll mod){
    a %= mod;
    int ans = 1;
    while(b){
        if(b & 1) ans = (ans * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return ans;
}

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5;
pair<pii,int> tr[4 * N];
int a[N], pos[N];
pair<pii,int> merge(pair<pii,int> p1, pair<pii,int> p2){
	pair<pii,int> ans;
	ans.se = p1.se + p2.se;
	if(p1.se & 1){
		ans.fi.fi = min(p1.fi.fi, p2.fi.se);
		ans.fi.se = min(p1.fi.se, p2.fi.fi);
	}
	else{
		ans.fi.fi = min(p1.fi.fi, p2.fi.fi);
		ans.fi.se = min(p1.fi.se, p2.fi.se);
	}
	return ans;
}
void build(int nd,int s,int e){
	if(s == e){
		tr[nd] = {{a[s], 1e18}, 1};
		return;
	}
	int m = (s + e) >> 1;
	build(nd << 1, s, m);
	build(nd << 1 | 1, m + 1, e);
	tr[nd] = merge(tr[nd << 1], tr[nd << 1 | 1]);
}
void remove(int nd,int s,int e,int idx){
	if(s == e){
		tr[nd] = {{1e18, 1e18}, 0};
		return;
	}
	int m = (s + e) >> 1;
	if(idx <= m)
		remove(nd << 1, s, m, idx);
	else
		remove(nd << 1 | 1, m + 1, e, idx);
	tr[nd] = merge(tr[nd << 1], tr[nd << 1 | 1]);
}
pair<pii,int> query(int nd,int s,int e,int l,int r){
	if(s > r || e < l)
		return {{1e18, 1e18}, 0};
	if(l <= s && e <= r){
		return tr[nd];
	}
	int m = (s + e) >> 1;
	auto it1 = query(nd << 1, s, m, l, r);
	auto it2 = query(nd << 1 | 1, m + 1, e, l, r);
	return merge(it1, it2);
}
set<int> done;
void solve(){
	int n;
	cin >> n;
	fr(i, 1, n){
		cin >> a[i];
		pos[a[i]] = i;
	}
	build(1, 1, n);
	vector<int> ans;
	done.insert(n + 1);
	fr(i, 1, n / 2){
		auto p1 = query(1, 1, n, 1, n);
		int val = p1.fi.fi;
		ans.pb(val);
		int r = *done.upper_bound(pos[val]) - 1;
		auto p = query(1, 1, n, pos[val] + 1, r);
		int val1 = p.fi.fi;
		ans.pb(val1);
		done.insert(pos[val]);
		done.insert(pos[val1]);
		remove(1, 1, n, pos[val]);
		remove(1, 1, n, pos[val1]);
	}
	for(int x : ans){
		cout << x << " ";
	}
}
signed main(){
    IO;
    #ifdef LOCAL
        freopen("inp.txt","r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    cout << setprecision(10) << fixed;
    int t = 1;
    // cin >> t;
    fr(tt, 1, t){
        solve();
    }
    return 0;
}

Measured memory_bytes: 28912