#include <bits/stdc++.h>
typedef int Elemtype;
typedef struct BiTNode
{
Elemtype data;
int depth;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
int max(int x,int y) //找两个数中的较大值
{
return x>y ? x:y;
}
int Deep(BiTree T) //求某棵树的深度
{
if (!T)
return 0;
else return T->depth;
}
BiTree LL(BiTree T)//右旋
{
BiTree p = T->lchild;
T->lchild = p->rchild;
p->rchild = T;
p->depth = max(Deep(p->lchild),Deep(p->rchild))+1;
T->depth = max(Deep(T->lchild),Deep(T->rchild))+1;
return p ;
}
BiTree RR(BiTree T)//左旋
{
BiTree p = T->rchild;
T->rchild = p->lchild;
p->lchild = T;
p->depth = max(Deep(p->lchild),Deep(p->rchild))+1;
T->depth = max(Deep(T->lchild),Deep(T->rchild))+1;
return p;
}
BiTree LR(BiTree T)
{
T->lchild = RR(T->lchild);//先做左旋
return LL(T);//在做右旋
}
BiTree RL(BiTree T)
{
T->rchild = LL(T->rchild);//先做右旋
return RR(T);//在做左旋
}
BiTree Create(BiTree T,int x)
{
if (T == NULL)
{
T = new(BiTNode);
T->data = x;
T->depth = 0;
T->rchild = NULL;
T->lchild = NULL;
}
else if (x < T->data )
{
T->lchild = Create(T->lchild,x);
if (abs(Deep(T->lchild) - Deep(T->rchild)) > 1)
{
if (x < T->lchild->data)
T = LL(T);
else
T = LR(T);
}
}
else if (x > T->data)
{
T->rchild = Create(T->rchild,x);
if (abs(Deep(T->rchild) - Deep(T->lchild)) > 1)
{
if (T->rchild->data > x)
T = RL(T);
else
T = RR(T);
}
}
T->depth = max(Deep(T->lchild),Deep(T->rchild)) + 1;
return T;
}
int main()
{
int n,m;
scanf ("%d",&n);
BiTree T = NULL;
for (int i=0; i<n; i++)
{
scanf ("%d",&m);
T = Create(T,m);
}
printf ("%d\n",T->data);
return 0;
}
typedef int Elemtype;
typedef struct BiTNode
{
Elemtype data;
int depth;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
int max(int x,int y) //找两个数中的较大值
{
return x>y ? x:y;
}
int Deep(BiTree T) //求某棵树的深度
{
if (!T)
return 0;
else return T->depth;
}
BiTree LL(BiTree T)//右旋
{
BiTree p = T->lchild;
T->lchild = p->rchild;
p->rchild = T;
p->depth = max(Deep(p->lchild),Deep(p->rchild))+1;
T->depth = max(Deep(T->lchild),Deep(T->rchild))+1;
return p ;
}
BiTree RR(BiTree T)//左旋
{
BiTree p = T->rchild;
T->rchild = p->lchild;
p->lchild = T;
p->depth = max(Deep(p->lchild),Deep(p->rchild))+1;
T->depth = max(Deep(T->lchild),Deep(T->rchild))+1;
return p;
}
BiTree LR(BiTree T)
{
T->lchild = RR(T->lchild);//先做左旋
return LL(T);//在做右旋
}
BiTree RL(BiTree T)
{
T->rchild = LL(T->rchild);//先做右旋
return RR(T);//在做左旋
}
BiTree Create(BiTree T,int x)
{
if (T == NULL)
{
T = new(BiTNode);
T->data = x;
T->depth = 0;
T->rchild = NULL;
T->lchild = NULL;
}
else if (x < T->data )
{
T->lchild = Create(T->lchild,x);
if (abs(Deep(T->lchild) - Deep(T->rchild)) > 1)
{
if (x < T->lchild->data)
T = LL(T);
else
T = LR(T);
}
}
else if (x > T->data)
{
T->rchild = Create(T->rchild,x);
if (abs(Deep(T->rchild) - Deep(T->lchild)) > 1)
{
if (T->rchild->data > x)
T = RL(T);
else
T = RR(T);
}
}
T->depth = max(Deep(T->lchild),Deep(T->rchild)) + 1;
return T;
}
int main()
{
int n,m;
scanf ("%d",&n);
BiTree T = NULL;
for (int i=0; i<n; i++)
{
scanf ("%d",&m);
T = Create(T,m);
}
printf ("%d\n",T->data);
return 0;
}