//构建数组二叉树
#include<iostream>
using namespace std;

#define MANX 32768
int tree[MANX];

int creat(int _tree[],int _node[],int len)//创建树 
{
   
	int i,MAX=1;
	_tree[1]=_node[1];
	int level=1;
	for(i=2;i<=len;i++){
   
		level=1;
		while(_tree[level]!=0){
   //所有数组均从一开始 
			if(_node[i]<_tree[level]){
   
				level=level*2;
			}
			else{
   
				level=level*2+1;
			}
			if(MAX<level){
        //判断最后一个元素占数组的下标 
				MAX=level;
			}
		}
		_tree[level]=_node[i];
	} 
	return MAX;
} 

int main(){
   
	freopen("arraytree.in","r",stdin);
	freopen("arraytree.out","w",stdout);
	int n,num;
	cin>>n;
	int node[n+1];
	for(int i=1;i<=n;i++){
   
		cin>>node[i];
	}
	num=creat(tree,node,n);
	for(int i=1;i<=n;i++){
   
		cout<<tree[i]<<endl;
	} 
	cout<<tree[num]<<endl;
	return 0;
}

//二叉树结构数组表示法 
#include<iotream>
using namespace std;

#define MAXN 32768 //2^15
int n;

struct tree{
   
	int Left;
	int Date;
	int Right;
};

typedef struct tree treenode;
typedef b_tree[MAXN];

void creat(int *node,int len){
   
	int i;
	int level;
	int position;
	for(i=1;i<=n;i++){
   
		b_tree[i].Date=node[i];
		level=0;
		position=0;
		while(position==0){
   
			if(node[i]>b_tree[level].Date){
   
				if(b_tree[level].Right!=-1){
   
					level=b_tree[level].Right;
				}else{
   
					position=-1;
				}
			}
			else{
   
				if(b_tree[level].Left!=-1){
   
					level=b_tree[level].Left;
				}else{
   
					position=1;
				}
			} 
		}
		if(position==1){
   
				b_tree[level].Left=i;
			}else{
   
				b_tree[level].Right=i; 
			}
	}
} 

void print()
{
   
	for(int i=1;i<=n;i++){
   
		cout<<b_tree[i].Left<<' '<<b_tree[level].Date<<' '<<b_tree[level].Right<<endl;
	}
}

int main()
{
   
	freopen("arraytree.in","r",stdin);
	freopen("arraytree.out","w",stdout);
	int i;
	cin>>n;
	int node[n+1];
	for(i=0;i<MAXN;i++)
		cin>>node[i];
	for(i=0;i<MAXN;i++){
   
		b_tree[i].Left=-1;
		b_tree[i].Date=0;
		b_tree[i].Right=-1; 
	}
	creat(node,n);
	print();
}