#include<cstdio>
#include<algorithm>
using namespace std;
struct node {
int val, height;
node *left, *right;
node(int v) {
val = v;
height = 1;
left = NULL;
right = NULL;
}
};
int getHeight(node *n) {
return n == NULL ? 0 : n->height;
}
void updateHeight(node *root) {
root->height = max(getHeight(root->left), getHeight(root->right)) + 1;
}
int getBalanceFactor(node *n) {
return getHeight(n->left) - getHeight(n->right);
}
void R(node *&root) {
node *temp = root->left;
root->left = root->left->right;
temp->right = root;
root = temp;
updateHeight(root->right); //旋转完记得更新结点高度
updateHeight(root);
}
void L(node *&root) {
node *temp = root->right;
root->right = root->right->left;
temp->left = root;
root = temp;
updateHeight(root->left);
updateHeight(root);
}
void insert(node * &root, int val) {
if (root == NULL) {
root = new node(val);
return;
}
if (val < root->val) { //插入到左子树中
insert(root->left, val);
updateHeight(root);
if (getBalanceFactor(root) == 2) {
if (getBalanceFactor(root->left) == 1) { //右单旋
R(root);
}
else if (getBalanceFactor(root->left) == -1) { //左右双旋
L(root->left);
R(root);
}
}
}
else {
insert(root->right, val);
updateHeight(root);
if (getBalanceFactor(root) == -2) {
if (getBalanceFactor(root->right) == -1) { //左单旋
L(root);
}
else if (getBalanceFactor(root->right) == 1) { //右左双旋
R(root->right);
L(root);
}
}
}
}
int main() {
int n;
scanf("%d", &n);
node *root = NULL;
for (int i = 0;i < n;i++) {
int temp;
scanf("%d", &temp);
insert(root, temp);
}
printf("%d", root->val);
}