//  main.cpp
//  solve
//
//  Created by Ahmed on 11/16/16.
//  Copyright © 2016 Abdellah. All rights reserved.
//


#include<set>
#include<map>
#include <unordered_map>
#include <unordered_set>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
#include<math.h>
#include <strings.h>



#define f first
#define s second
#define pb push_back
#define sz(a) (int)a.size()
#define lp(i,a,n) for(int (i)=(a);(i)<=(int)(n);(i)++)
#define lpd(i,n,a) for(ll (i)=(n);(i)>=(a);--(i))
#define mp make_pair
#define clr(a,b) memset(a,b,sizeof a)
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define eps 1e-6
#define infi 1000000002
#define infll 1000000000000000000ll
#define MX 1000000
#define X real()
#define Y imag()
#define polar(r,t) ((r)* exp(point(0,(t))))
#define length(a) hypot( (a).X , (a).Y )
#define angle(a) atan2( (a).Y , (a).X )
#define vec(a,b) ( (b) - (a) )
#define dot(a,b) (conj(a)*(b)).X
#define cross(a,b) (conj(a)*(b)).Y
#define lengthsqr(a) dot(a,a)
#define reflect(V,M) (conj((V)/(M)) * (M))
#define normalize(a)   ((a)/length(a))
#define ccw(a,b,c)   cross(vec(a,b) , vec(a,c)) > -eps
#define cosRule(a,b,c) (acos(((a)*(a)+(b)*(b)-(c)*(c))/(2*(a)*(b))))
#define cosDot(a,b) (acos(dot(a,b)/length(a)/length(b)))
#define EQ(a,b) (fabs((a) - (b)) <= eps) /* equal to */
#define NE(a,b) (fabs((a) - (b)) > eps)  /* not equal to */
#define LT(a,b) ((a) < (b) - eps)        /* less than */
#define GT(a,b) ((a) > (b) + eps)        /* greater than */
#define LE(a,b) ((a) <= (b) + eps)       /* less than or equal to */
#define GE(a,b) ((a) >= (b) - eps)       /* greater than or equal to */
#define mod1 100050001
#define mod2 100030001
#define base1 37
#define base2 31
#define que priority_queue<pair<int,int>, vector<pair<int,int>> , greater<pair<int,int> > >
#define rotate(v,t) ((v)*exp(point(0,t)))
#define rotateabout(v,t,a) (rotate(vec(a,v),t)+(a))
#define PI atan(1)*4

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef set<int> si;
typedef map<int, int> mii;
typedef complex<long double> point;
typedef pair<point, point> line;
typedef pair<double , point> Circle;

const int N = 200005;
int n,a[N];
pii tree[4*N];
bool rev[4*N];


void propagate(int l,int r,int i) {
    if(!rev[i]) return;
    
    rev[i] = 0;
    swap(tree[i].f, tree[i].s);
    rev[i<<1] ^= 1 , rev[(i<<1)|1] ^= 1;
    
}

void build(int l,int r,int i) {
    if(l == r) {
        if(l&1) tree[i] = {l,n+1};
        else tree[i] = {n+1,l};
        return;
    }
    
    int mid = (l+r)>>1;
    build(l, mid, i<<1) , build(mid+1, r, (i<<1)|1);
    
    if(a[tree[i<<1].f]  < a[tree[(i<<1)|1].f])
        tree[i].f = tree[i<<1].f;
    else
        tree[i].f = tree[(i<<1)|1].f;
    

    
    if(a[tree[i<<1].s]  < a[tree[(i<<1)|1].s])
        tree[i].s = tree[i<<1].s;
    else
        tree[i].s = tree[(i<<1)|1].s;
    
}

void removee(int l,int r,int i,int x) {
    
    propagate(l, r, i);
    
    if(l == r){
        tree[i] = {n+1,n+1};
        return;
    }
    
    int mid = (l+r)>>1;
    if(mid >= x) removee(l,mid,i<<1,x);
    else removee(mid+1, r, (i<<1)|1, x);
    
    if(a[tree[i<<1].f]  < a[tree[(i<<1)|1].f])
        tree[i].f = tree[i<<1].f;
    else
        tree[i].f = tree[(i<<1)|1].f;
    
    
    
    if(a[tree[i<<1].s]  < a[tree[(i<<1)|1].s])
        tree[i].s = tree[i<<1].s;
    else
        tree[i].s = tree[(i<<1)|1].s;
    
}

void flip (int l,int r,int i,int x,int y) {
    propagate(l, r, i);
    
    if(l > y || r < x) return ;
    
    if(l >= x && r <= y) {
        rev[i] ^= 1;
        propagate(l, r, i);
        return ;
    }
    int mid = (l+r)>>1;
    
    flip(l, mid, i<<1, x, y) , flip(mid+1, r, (i<<1)|1, x, y);
    
    if(a[tree[i<<1].f]  < a[tree[(i<<1)|1].f])
        tree[i].f = tree[i<<1].f;
    else
        tree[i].f = tree[(i<<1)|1].f;
    
    
    
    if(a[tree[i<<1].s]  < a[tree[(i<<1)|1].s])
        tree[i].s = tree[i<<1].s;
    else
        tree[i].s = tree[(i<<1)|1].s;
    
}

int query(int l,int r,int i, int x,int y , bool t) {
    propagate(l, r, i);
    
    if(l > y || r < x) return n+1;
    
    if(l >= x && r <= y) return t ? tree[i].f:tree[i].s;
    
    int mid = (l+r)>>1;
    
    int r1 = query(l, mid, i<<1, x, y, t);
    int r2 = query(mid+1, r, (i<<1)|1, x, y, t);
    
    if(a[r1]  < a[r2]) return r1;
    return r2;
}

void mergeee(vector<pii> &a,vector<pii> &b, vector<pii> &ret) {
    
    int i = 0, j = 0;
    
    while(i < sz(a) && j < sz(b)) {
        if(a[i] < b[j]) ret.pb(a[i++]);
        else ret.pb(b[j++]);
    }
    
    while(i < sz(a)) ret.pb(a[i++]);
    
    while(j < sz(b)) ret.pb(b[j++]);
    
}

pii solve(int l,int r ) {
    if(l >= r) return {n+1,n+1};
    
    if(!(l&1)) {
        int x = query(1, n,  1, l, r, false);
        int tx = query(1, n, 1, x, r, true);
        return {x,tx};
    }
    
    int y = query(1, n, 1, l, r, true);
    int ty =  query(1, n, 1, y, r, false);

    return {y,ty};
    
}


int main()
{
    scanf("%d",&n);
    lp(i, 1, n)scanf("%d",&a[i]);
    a[n+1] = infi;
    
    build(1, n, 1);
    priority_queue<pair< pii , pii> , vector<pair<pii, pii> > , greater<pair<pii, pii> > > q;
    vi ans;
    int l = 1,r = n;
    
    
    pii temp = solve(l, r);
    q.push({{a[temp.f],a[temp.s]} , {l,r}});
        
    while(!q.empty()) {
        
        pair<pii,pii> cur = q.top();
        q.pop();
        
        if(cur.f.f == infi || cur.f.s == infi) continue;
        int l = cur.s.f , r = cur.s.s;
        ans.pb(cur.f.f) , ans.pb(cur.f.s);

        
        
        pii temp = solve(l,r);
        
        
        pii temp1 = solve(l,temp.f-1);
        
        q.push({{a[temp1.f],a[temp1.s]},{l,temp.f-1}});
        
        
        temp1 = solve(temp.f+1 , temp.s-1);
        
        q.push({{a[temp1.f],a[temp1.s]},{temp.f+1 , temp.s-1}});
        
        
        
        temp1 = solve(temp.s+1,r);
        
        q.push({{a[temp1.f],a[temp1.s]},{temp.s+1,r}});
        
    }
    
    
    for(int x:ans) printf("%d ",x);
    printf("\n");
    
    return 0;
}
/*
 
 */
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);
//ios::sync_with_stdio(0);cin.tie(0);



Measured memory_bytes: 13552