#include<bits/stdc++.h>
#define reg register
#define maxn 400001
#define inf 998244353
using namespace std;
inline int  read() {
    reg int s = 0, t = 0; reg char ch = getchar();
    while(ch > '9' || ch < '0') t |= ch == '-', ch = getchar();
    while(ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return t ? -s : s;
}
int minn[2][maxn << 2], d[maxn], a[maxn], pos[maxn], n, tag[maxn << 2], lim[maxn << 2], limt;
void push_down(int k){
    tag[k << 1] ^= 1; tag[k << 1 | 1] ^= 1;
    swap(minn[1][k << 1], minn[0][k << 1]);
    swap(minn[1][k << 1 | 1], minn[0][k << 1  | 1]);
    tag[k] = 0;
}
void update(int opt, int k, int l, int r, int pos, int x){
    int mid = l + r >> 1;
    if(l == r){
        minn[opt][k] = x; return; 
    }
    if(tag[k]) push_down(k);
    if(pos <= mid) update(opt, k << 1, l , mid, pos, x);
    else update(opt, k << 1 | 1, mid + 1, r, pos, x);
    if(minn[0][k << 1] > 0 && minn[0][k << 1 | 1] > 0)
    minn[0][k] = min(minn[0][k << 1], minn[0][k << 1 | 1]);
    else minn[0][k] = minn[0][k << 1] | minn[0][k << 1 | 1]; 
    if(minn[1][k << 1] > 0 && minn[1][k << 1 | 1] > 0)
    minn[1][k] = min(minn[1][k << 1], minn[1][k << 1 | 1]);
    else minn[1][k] = minn[1][k << 1] | minn[1][k << 1 | 1]; 

}
void modify(int k, int l, int r, int L, int R){
    int mid = l + r >> 1;
    if(L <= l && r <= R){
        swap(minn[0][k], minn[1][k]); tag[k] ^= 1;
        return;
    }
    if(tag[k]) push_down(k);
    if(L <= mid) modify(k << 1, l, mid, L, R);
    if(R > mid) modify(k << 1 | 1, mid + 1, r, L, R);
    if(minn[0][k << 1] > 0 && minn[0][k << 1 | 1] > 0)
    minn[0][k] = min(minn[0][k << 1], minn[0][k << 1 | 1]);
    else minn[0][k] = minn[0][k << 1] | minn[0][k << 1 | 1]; 
    if(minn[1][k << 1] > 0 && minn[1][k << 1 | 1] > 0)
    minn[1][k] = min(minn[1][k << 1], minn[1][k << 1 | 1]);
    else minn[1][k] = minn[1][k << 1] | minn[1][k << 1 | 1]; 
}

int query(int k, int l , int r, int L, int R, int opt){
    int ansx = 0, ansy = 0;
    if(L <= l && r <= R){
        ansx = minn[opt][k];
        return ansx; 
    }
    if(tag[k]) push_down(k);
    int mid = l + r >> 1;
    if(L <= mid) ansx = query(k << 1, l, mid, L, R, opt);
    if(R > mid) ansy = query(k << 1 | 1, mid + 1, r, L, R, opt);
    if(ansx > 0 && ansy > 0) return min(ansx, ansy);
    else return (ansx | ansy);
    if(minn[0][k << 1] > 0 && minn[0][k << 1 | 1] > 0)
    minn[0][k] = min(minn[0][k << 1], minn[0][k << 1 | 1]);
    else minn[0][k] = minn[0][k << 1] | minn[0][k << 1 | 1]; 
    if(minn[1][k << 1] > 0 && minn[1][k << 1 | 1] > 0)
    minn[1][k] = min(minn[1][k << 1], minn[1][k << 1 | 1]);
    else minn[1][k] = minn[1][k << 1] | minn[1][k << 1 | 1]; 
}
void query_pos(int k ,int l, int r, int x){
    int mid = l + r >> 1;
    if(lim[k]) limt = min(lim[k], limt);
    if(l == r) return;
    if(x <= mid) query_pos(k << 1, l, mid, x);
    else query_pos(k << 1 | 1, mid + 1, r, x);
}
void update_pos(int k, int l, int r, int L, int R, int x){
    if(L <= l && r <= R){
        if(!lim[k])lim[k] = x;
        else lim[k] = min(lim[k], x); 
        return;
    }
    int mid = l + r >> 1;
    if(mid >= L) update_pos(k << 1, l, mid, L, R, x);
    if(R > mid) update_pos(k << 1 | 1, mid + 1, r, L ,R , x);
}
int main()
{
   // freopen("1.in", "r", stdin);
    n = read();
    for(int i = 1; i <= n; i++){
        a[i] = read(); pos[a[i]] = i;
        if(i & 1) update(1, 1, 1, n, i, a[i]);
        else update(0, 1, 1, n, i, a[i]);
    }
    int t = n / 2;
    while(t--){
        int ans_x = minn[1][1]; limt = n;
        query_pos(1, 1, n, pos[ans_x]);
        int ans_y = query(1, 1, n, pos[ans_x] + 1, limt, 0);
        update(1, 1, 1, n, pos[ans_x], 0); 
        update(0, 1, 1, n, pos[ans_y], 0);
        modify(1, 1, n, pos[ans_x] + 1, pos[ans_y] - 1);
        update_pos(1, 1, n, 1, pos[ans_x], pos[ans_x]);
        update_pos(1, 1, n, pos[ans_x], pos[ans_y], pos[ans_y]);
        printf("%d %d ", ans_x, ans_y);
    }
}

Measured memory_bytes: 22016