//A,Elebereth Gilthoniel mantae!
    //For Temeria!
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<set>
    #include<cstdio>
    #include<map>
    #include<stack>
    #include<queue>
    #include<cstring> 
    using namespace std;
    long long n,q[200008],p[200008];long long pos[200008];long long uj;vector<long long>v[600008];long long u;
    long long treeo[800008],treee[800008];pair<long long,long long >pa[600008];
    void update(long long x,long long y,long long t)
    {
    	if(t%2)
    	{
    		treeo[y]=x;
    		while(y>0)
    		{
    			y=y/2;
    			treeo[y]=min(treeo[y],x);
    		}
    	}
    	else
    	{
    		treee[y]=x;
    		while(y>0)
    		{
    			y=y/2;
    			treee[y]=min(treee[y],x);
    		}
    	}
    }
      long long queryo(long long node, long long begin, long long end, long long left, long long right)    
    {   
       	long long p1, p2;    
    	if (left > end || right < begin)    return -1;      
       if (begin >= left && end <= right)    
                return treeo[node];    
            p1 = queryo(2 * node, begin, (begin + end) / 2, left, right);   
            p2 = queryo(2 * node + 1, (begin + end) / 2 + 1, end, left, right);     
            if (p1 == -1)    
                return p2;    
            if (p2 == -1)    
                return p1;    
            if (p1 <= p2)    
                return  p1;    
            return  p2;      
    }  
    long long querye(long long node, long long begin, long long end, long long left, long long right)    
    {  
       	long long p1, p2;    
    	if (left > end or right <begin)    return -1;      
       if (begin >= left && end <= right)    
                return treee[node];    
            p1 = querye(2 * node, begin, (begin + end) / 2, left, right);   
            p2 = querye(2 * node + 1, (begin + end) / 2 + 1, end, left, right);     
            if (p1 == -1)    
                return p2;    
            if (p2 == -1)    
                return p1;    
            if (p1 <= p2)    
                return  p1;    
            return  p2;      
    }  
    long long solve(long long l,long long r)
    {
    	if(l>=r) return 0;
    	if(r==l+1)
    	{
    		pa[l]=(make_pair(-p[l],-p[r]));
    		return l;
    	}
    	long long mi=0;if(l%2) mi=queryo(1,1,u,l,r-1);else mi=querye(1,1,u,l,r-1);
    	long long mii=0;if(pos[mi]%2) mii=querye(1,1,u,pos[mi]+1,r);else mii=queryo(1,1,u,pos[mi]+1,r);
    	pa[pos[mi]]=make_pair(-mi,-mii);int o=pos[mi];	
    	v[o].push_back(solve(l,pos[mi]-1));v[o].push_back(solve(pos[mi]+1,pos[mii]-1));v[o].push_back(solve(pos[mii]+1,r));
		return pos[mi];
    }

    pair<long long,pair<long long,long long> > aap[200008];
    void dfs(int x)
    {
    	priority_queue<pair<long long,long long> > w;
    	w.push(pa[x]);
    	while(w.empty()==0)
    	{
    		pair<long long,long long>o=w.top();long long d=pos[-o.first];cout<<-o.first<<" "<<-o.second<<" ";w.pop();
			for(long long i=0;i<v[d].size();i++) if(v[d][i]!=0)w.push(pa[v[d][i]]);
    	}
    	
    }
    int main()
    {
     	//freopen("","r",stdin);
    	//freopen("","w",stdout);
    	for(long long i=0;i<800001;i++) treeo[i]=treee[i]=123456789;cin>>n;
    	u=1;while(u<n) u*=2;
    	for(long long i=1;i<=n;i++) scanf("%d",&p[i]),pos[p[i]]=i,update(p[i],i+u-1,i);
    	dfs(solve(1,n));
		cout<<endl;
    	return 0;
    }
    //4 4 1 3 2

Measured memory_bytes: 49408