// Slower C++ solution:
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<string>

#include<math.h>

#include<vector>

#include<queue>

#include<map>

#include<stack>

#include<fstream>

#include<stdlib.h>

#include<set>

#include<climits>

#include<cmath>  

#include<memory.h> 

#include<sstream>

#include<time.h>

#include<iomanip>

using namespace std;

const unsigned long long BIGEST=1000000000000000000+1000000000000000000;

const long long BIGER=1000000000000000000;

const int BIG=1000000000;

const int MOD=1000000007;

const long long LMOD=1000000007;

#define F first

#define S second

#define p2 pair<int,int>

#define cb make_pair

int n;

int ar[200005];

vector<int>v[200005];

p2 p[200005];

struct seg

{

	int mnid[800005][2];

	int ch(int x,int l=1,int r=n,int k=1)

	{

		if(r<x||x<l)return 0;

		//cout<<"!"<<x<<" "<<l<<" "<<r<<" "<<k<<" ";

		if(ar[x]<ar[mnid[k][x%2]])mnid[k][x%2]=x;

		//cout<<mnid[k][0]<<" "<<mnid[k][1]<<endl;

		if(l!=r)

		{

			ch(x,l,(l+r)/2,k*2);

			ch(x,(l+r)/2+1,r,k*2+1);

		}

		//cout<<"!"<<x<<" "<<l<<" "<<r<<" "<<k<<" ";

		//cout<<mnid[k][0]<<" "<<mnid[k][1]<<endl;

		return 0;

	}

	int q(int a,int b,int v,int l=1,int r=n,int k=1)

	{

		//cout<<" "<<a<<" "<<b<<" "<<v<<" "<<l<<" "<<r<<" "<<k<<endl;

		if(r<a||b<l)return 0;

		//cout<<">"<<a<<" "<<b<<" "<<v<<" "<<l<<" "<<r<<" "<<k<<endl;

		if(a<=l&&r<=b)return mnid[k][v];

		if(l!=r)

		{

			int t1,t2;

			t1=q(a,b,v,l,(l+r)/2,k*2);

			t2=q(a,b,v,(l+r)/2+1,r,k*2+1);

			if(ar[t1]<ar[t2])return t1;

			else return t2;

		}

		return 0;

	}

}T;

int g=0;

int fu(int l,int r)

{

	//cout<<l<<" "<<r<<endl;

	int id=++g;

	p2 t;

	t.F=T.q(l,r,l%2);

	t.S=T.q(t.F,r,(l+1)%2);

	//cout<<"("<<t.F<<","<<t.S<<")";

	if(t.F>l)v[id].push_back(fu(l,t.F-1));

	if(t.S>t.F+1)v[id].push_back(fu(t.F+1,t.S-1));

	if(r>t.S)v[id].push_back(fu(t.S+1,r));

	p[id].F=ar[t.F];

	p[id].S=ar[t.S];

	return id;

}

int main()

{

	ar[0]=BIG;

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&ar[i]);

		T.ch(i);

	}

	fu(1,n);

	set<pair<p2,int > >q;

	q.insert(cb(p[1],1));

	pair<p2,int> t;

	while(!q.empty())

	{

		t=*q.begin();

		q.erase(t);

		printf("%d %d ",t.F.F,t.F.S);

		//cout<<t.S<<" ";

		for(int i=0;i<v[t.S].size();i++)q.insert(cb(p[v[t.S][i]],v[t.S][i]));

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 6.464747895096043x):
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5+20 , INF = 0x3f3f3f3f;

int n,a[N],odd[N<<2],even[N<<2]; // even偶数 , odd奇数

inline void update(int &x,int y) { if ( a[x] > a[y] ) x = y; }

inline void build(int p,int l,int r)

{

    if ( l == r )

        return ( ( l & 1 ) ? odd[p] = l : even[p] = l ) , void();

    int mid = l + r >> 1;

    build( p<<1 , l , mid ) , build( p<<1|1 , mid+1 , r );

    update( odd[p] , odd[p<<1] ) , update( odd[p] , odd[p<<1|1] );

    update( even[p] , even[p<<1] ) , update( even[p] , even[p<<1|1] );

}

inline int query(int p,int l,int r,int ql,int qr,int op)

{

    if ( ql <= l && qr >= r ) return op ? even[p] : odd[p];

    int mid = l + r >> 1 , res = 0;

    if ( ql <= mid ) update( res , query(p<<1,l,mid,ql,qr,op) );

    if ( qr > mid ) update( res , query(p<<1|1,mid+1,r,ql,qr,op) );

    return res;

}

struct sec

{

    int l,r,ansl,ansr,op;

    sec (int _l,int _r,int _op)

    {

        l = _l , r = _r , op = _op;

        ansl = query(1,1,n,l,r,op);

        ansr = query(1,1,n,ansl+1,r,op^1);

    }

    friend bool operator < (sec x,sec y)

    {

        if ( a[x.ansl] == a[y.ansl] )

            return a[x.ansr] < a[y.ansr];

        return a[x.ansl] < a[y.ansl];

    }

};

set <sec> S;

inline void solve(void)

{

    S.insert( sec(1,n,0) );

    while ( S.size() )

    {

        sec now = *S.begin(); S.erase(now);

        printf("%d %d ",a[now.ansl],a[now.ansr]);

        if ( now.l < now.ansl-1 ) S.insert(sec(now.l,now.ansl-1,now.op));

        if ( now.ansl+1 < now.ansr-1 ) S.insert(sec(now.ansl+1,now.ansr-1,now.op^1));

        if ( now.ansr+1 < now.r ) S.insert(sec(now.ansr+1,now.r,now.op));

    }

}

inline void input(void)

{

    scanf("%d",&n);

    for (int i=1;i<=n;i++)

        scanf("%d",&a[i]);

}

int main(void)

{

    input();

    a[0] = INF;

    build( 1 , 1 , n );

    solve();

    return puts("") , 0;

}