#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
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();
}