#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include <map>
#include <set>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <fstream>
#include <bitset>
#define int long long
using namespace std;
int n, ai;
vector<int> data;
struct Vertex{int minimum; int antiminimum; bool pushs;};
vector<Vertex> rmq;
int K = 1e9;
void create(int i, int l, int r){
    if (r - l == 1){
        if (l % 2 == 0){
            rmq[i].minimum = data[l];
        }
        else rmq[i].antiminimum = data[l];
        return;
    }
    int mid = (l + r) / 2;
    create(2*i+1, l, mid);
    create(2*i+2, mid, r);
    rmq[i].minimum = min(rmq[2*i+1].minimum, rmq[2*i+2].minimum);
    rmq[i].antiminimum = min(rmq[2*i+1].antiminimum, rmq[2*i+2].antiminimum);
}
void push(int i, int l, int r){
    if (l + 1 >= r) return;
    if (!rmq[i].pushs) return;
    swap(rmq[2*i+1].minimum, rmq[2*i+1].antiminimum);
    swap(rmq[2*i+2].minimum, rmq[2*i+2].antiminimum);
    rmq[2*i+1].pushs ^= 1;
    rmq[2*i+2].pushs ^= 1;
    rmq[i].pushs = false;
}
int get(int i, int l, int r, int l1, int r1){
    push(i, l, r);
    if (l1 >= r1) return K;
    if (l == l1 && r == r1){
        return rmq[i].antiminimum;
    }
    int mid = (l + r) / 2;
    return min(get(2*i+1, l, mid, l1, min(r1, mid)), get(2*i+2, mid, r, max(l1, mid), r1));
}
void change(int i, int l, int r, int l1, int r1){
    push(i, l, r);
    if (l1 >= r1) return;
    if (l == l1 && r == r1){
        swap(rmq[i].minimum, rmq[i].antiminimum);
        rmq[i].pushs = true;
        return;
    }
    int mid = (l + r) / 2;
    change(2*i+1, l, mid, l1, min(r1, mid));
    change(2*i+2, mid, r, max(l1, mid), r1);
    rmq[i].minimum = min(rmq[2*i+1].minimum, rmq[2*i+2].minimum);
    rmq[i].antiminimum = min(rmq[2*i+1].antiminimum, rmq[2*i+2].antiminimum);
}
void del(int i, int l, int r, int ind){
    push(i, l, r);
    if (r - l == 1){
        rmq[i].antiminimum = K;
        rmq[i].minimum = K;
        return;
    }
    int mid = (l + r) / 2;
    if (ind < mid) del(2*i+1, l, mid, ind);
    else del(2*i+2, mid, r, ind);
    rmq[i].minimum = min(rmq[2*i+1].minimum, rmq[2*i+2].minimum);
    rmq[i].antiminimum = min(rmq[2*i+1].antiminimum, rmq[2*i+2].antiminimum);
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    set<int> nots;
    nots.insert(n);
    vector<int> where, ans;
    for (int i=0; i < n; i++){
        where.push_back(0);
        ans.push_back(0);
    }
    for (int i=0; i < n; i++){
        cin >> ai;
        data.push_back(ai);
        where[ai-1] = i;
    }
    for (int i=0; i < 4*n; i++){
        Vertex v = {K, K, false};
        rmq.push_back(v);
    }
    create(0, 0, n);
    for (int i=0; i < n/2; i++){
        int res = rmq[0].minimum;
        ans[2*i] = res;
        int next = *nots.upper_bound(where[res-1]);
        int g = get(0, 0, n, where[res-1] + 1, next);
        int ind = where[g-1];
        ans[2*i+1] = g;
        del(0, 0, n, where[res-1]);
        del(0, 0, n, ind);
        change(0, 0, n, where[res-1] + 1, ind);
        nots.insert(where[res-1]);
        nots.insert(ind);
    }
    for (int i=0; i < n; i++){
        cout << ans[i] << " ";
    }
    return 0;
}

Measured memory_bytes: 36180