pat A1066
#include<iostream>
using namespace std;
int n;
struct node
{
int weight;
int hight;
node *lchild;
node *rchild;
}*root;
int getshugao(node *root)
{
if(root==NULL)
return 0;
return root->hight;
}
void updateshugao(node *root)
{
root->hight=max(getshugao(root->lchild),getshugao(root->rchild))+1;
}
int factor(node *root)
{
return getshugao(root->lchild)-getshugao(root->rchild);
}
void L(node *&root)
{
node *temp=root->rchild;
root->rchild=temp->lchild;
temp->lchild=root;
updateshugao(temp);
updateshugao(root);
root=temp;
}
void R(node *&root)
{
node *temp=root->lchild;
root->lchild=temp->rchild;
temp->rchild=root;
updateshugao(temp);
updateshugao(root);
root=temp;
}
void insertL(node *&root,int x)
{
if(root==NULL)
{
root=new node;
root->weight=x;
root->hight=1;
root->lchild=root->rchild=NULL;
return ;
}
if(root->weight>x)
{
insertL(root->lchild,x);
updateshugao(root);
if(factor(root)==2)
{
if(factor(root->lchild)==1)
R(root);
else if(factor(root->lchild)==-1)
{
L(root->lchild);
R(root);
}
}
}
else
{
insertL(root->rchild,x);
updateshugao(root);
if(factor(root)==-2)
{
if(factor(root->rchild)==-1)
{
L(root);
}
else if(factor(root->rchild)==1)
{
R(root->rchild);
L(root);
}
}
}
}
int main()
{
cin >> n;
int temp;
for(int i=0; i<n; i++)
{
cin >> temp;
insertL(root,temp);
}
cout << root->weight;
return 0;
}