#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    int memo=modpow(a,b/2);
    return memo*memo%mod;
}
vector<int>kaijo,invkaijo;
void init_fact(int n){
    kaijo.resize(n+1);
    invkaijo.resize(n+1);
    kaijo[0]=1;
    for(int i=1;i<=n;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
    rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2);
}
int comb(int a,int b){
    if(a<b)return 0;
    if(a<0||b<0)return 0;
    return kaijo[a]*modpow(kaijo[a-b],mod-2)%mod*modpow(kaijo[b],mod-2)%mod;
}
int inv(int x){
    x=modpow(x,mod-2);
    return x;
}
struct SparseTable{
    vector<vector<P>>table;
    vector<int>logtable;
    void init(vector<int>v){
        int len=0;
        while((1<<len)<=v.size())len++;
        table.assign(len,vector<P>(1<<len));
        rep(i,v.size())table[0][i]={v[i],i};
        for(int i=1;i<len;i++){
            for(int j=0;j+(1<<i)<=(1<<len);j++){
                table[i][j]=min(table[i-1][j],table[i-1][j+(1<<(i-1))]);
            }
        }
        logtable.resize(v.size()+1);
        for(int i=2;i<logtable.size();i++){
            logtable[i]=logtable[(i>>1)]+1;
        }
    }
    P query(int l,int r){
        int len=logtable[r-l];
        return min(table[len][l],table[len][r-(1<<len)]);
    }
};
vector<SparseTable>st(2);
vector<vector<int>>ans;
void func(int x,int y){
    //[x,y)
    P l=st[x%2].query(x,y);
    P r=st[(x+1)%2].query(l.se+1,y);
    int pos=ans.size();
    ans.pb({l.fi,r.fi});
    if(x<l.se){
        ans[pos].pb(ans.size());
        func(x,l.se);
    }
    if(l.se+1<r.se){
        ans[pos].pb(ans.size());
        func(l.se+1,r.se);
    }
    if(r.se+1<y){
        ans[pos].pb(ans.size());
        func(r.se+1,y);
    }
}
void solve(){
    int n,p[200010];
    cin>>n;
    vector<vector<int>>v(2,vector<int>(n,INF));
    rep(i,n){
        cin>>p[i];
        v[i%2][i]=p[i];
    }
    rep(i,2)st[i].init(v[i]);
    func(0,n);
    priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>q;
    q.push(ans[0]);
    while(!q.empty()){
        vector<int>vec=q.top();
        q.pop();
        cout<<vec[0]<<" "<<vec[1]<<" ";
        for(int i=2;i<vec.size();i++)q.push(ans[vec[i]]);
    }
    cout<<endl;
}
signed main(){
    cin.tie(0);ios::sync_with_stdio(false);
    //int t;cin>>t;while(t--)
    solve();
    return 0;
}


Measured memory_bytes: 175584