/*input
6
25 1 2 3 14 36

*/
#include<bits/stdc++.h>
#include<algorithm>
#include <fstream>
#include <sstream>
#include <string>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define is insert
#define rep1(i,a,b) for(long long i=a;i<=b;i++)
#define mp make_pair
#define mh make_heap
#define F first
#define S second
//#define fin cin
//#define fout cout
#define file ifstream fin("input.txt");ofstream fout("output.txt");
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fr(n) for(long long i=0;i<n;i++)
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define ALL(x) (x).begin(), (x).end()
typedef long long int ll;
typedef long double ld;
#define lb lower_bound
#define ub uper_bound
#define bs lower_bound
//#define db(i) cout<<i<<endl;
typedef pair<ll,ll> pairs;
typedef vector<ll> vi;
typedef vector<pairs> vpair;
//find_by_order()  // order_of_key
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag,
tree_order_statistics_node_update> pbds;
struct compare{
  bool operator()(const pairs &a, const pairs &b)const{
    if(a.first == b.first) return a.second < b.second;
    return a.first > b.first;
  }
};

vi v[500000],link(500000),siz(500000);
map<ll,ll>ind;
multiset<ll>ms;
ll max_siz,min_val;

ll fin(ll x)
{
	if(link[x]==x)
		return x;
	return link[x]=fin(link[x]);
}

void connect(ll a,ll b)
{
	//cout<<"come :"<<a<<" "<<b<<endl;
	a=fin(a);
	if(link[b]!=0)
		b=fin(b);
	//cout<<"par "<<a<<" "<<b<<endl;
	if(siz[a]<siz[b])swap(a,b);
	siz[a]+=siz[b];
	link[b]=a;
}

void solve()
{
	ll n;cin>>n;
	vi v(n);
	fr(n+1)siz[i]=1;
	rep(i,0,n)
	{
		cin>>v[i];
		ind[v[i]]=i+1;
	}
	sort(ALL(v));
	max_siz=0;
	rep(i,0,n)
	{
		//cout<<"i:"<<i<<endl;
		if(!ms.empty())
		{
			if(*ms.begin()==(*(--ms.end())))
			{
				//cout<<"here"<<endl;
				if(max_siz<ms.size())
				{
					max_siz=ms.size();
					min_val=v[i];
				}
			}
		}
		ll k=ind[v[i]];
		//cout<<"k :"<<k<<endl;
		if(link[k-1]==0 && link[k+1]==0)
			link[k]=k;
		else
		{
			//cout<<"connecting"<<endl;
			if(link[k-1]!=0)
			{
				//cout<<"called "<<k-1<<endl;
				//cout<<"called "<<k-1<<" "<<fin(k-1)<<" "<<siz[fin(k-1)]<<" "<<ms.size()<<" "<<*ms.begin()<<endl;
				ms.erase(ms.find(siz[fin(k-1)]));
				connect(k-1,k);
			}
			if(link[k+1]!=0)
			{
				//cout<<"called "<<k+1<<" "<<fin(k+1)<<" "<<siz[fin(k+1)]<<" "<<ms.size()<<" "<<*ms.begin()<<endl;
				ms.erase(ms.find(siz[fin(k+1)]));
				connect(k+1,k);
			}
			//cout<<"connected"<<endl;
		}
		//cout<<"link[k] "<<link[k]<<endl;
		ms.is(siz[fin(k)]);
		/*cout<<"siz: "<<ms.size()<<endl;
		if(!ms.empty())
		{
			multiset<ll>::iterator it=ms.begin();
			for(it;it!=ms.end();it++)
				cout<<*it<<" ";
		}
		cout<<endl<<endl;*/
	}
	cout<<min_val<<endl;
}
int main()
{
	fast;
    ll t=1;
    for(int i=0;i<t;i++)
    	solve();
}
//10-b-0
//11-b-friction


/*input
8

1 2 7 3 4 8 5 6
*/
#include<bits/stdc++.h>
#include<algorithm>
#include <fstream>
#include <sstream>
#include <string>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define is insert
#define rep1(i,a,b) for(long long i=a;i<=b;i++)
#define mp make_pair
#define mh make_heap
#define F first
#define S second
//#define fin cin
//#define fout cout
#define file ifstream fin("input.txt");ofstream fout("output.txt");
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fr(n) for(long long i=0;i<n;i++)
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define ALL(x) (x).begin(), (x).end()
typedef long long int ll;
typedef long double ld;
#define lb lower_bound
#define ub uper_bound
#define bs lower_bound
//#define db(i) cout<<i<<endl;
typedef pair<ll,ll> pairs;
typedef vector<ll> vi;
typedef vector<pairs> vpair;
//find_by_order()  // order_of_key
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag,
tree_order_statistics_node_update> pbds;
struct compare{
  bool operator()(const pairs &a, const pairs &b)const{
    if(a.first == b.first) return a.second < b.second;
    return a.first > b.first;
  }
};

vi link(500000),siz(500000);
map<ll,ll>ind;
multiset<ll>ms;
ll max_siz,min_val;

ll fin(ll x)
{
	if(link[x]==x)
		return x;
	return link[x]=fin(link[x]);
}

void connect(ll a,ll b)
{
	a=fin(a);
	b=fin(b);
	if(siz[a]<siz[b])swap(a,b);
	siz[a]+=siz[b];
	link[b]=a;
}

void solve()
{
	ll n;cin>>n;
	vi v(n);
	rep(i,1,n+1)siz[i]=1;
	rep(i,0,n)
	{
		cin>>v[i];
		ind[v[i]]=i+1;
	}
	sort(ALL(v));
	max_siz=0;
	rep(i,0,n)
	{
		if(!ms.empty())
		{
			if(*ms.begin()==(*(--ms.end())))
			{
				if(max_siz<ms.size())
				{
					max_siz=ms.size();
					min_val=v[i];
				}
			}
		}
		ll k=ind[v[i]];
		link[k]=k;
		if(link[k-1]!=0 && k>1)
		{
			ms.erase(ms.find(siz[fin(k-1)]));
			connect(k-1,k);
		}
		if(link[k+1]!=0 && k<n)
		{
			ms.erase(ms.find(siz[fin(k+1)]));
			connect(k+1,k);
		}
		ms.is(siz[fin(k)]);
	}
	if(!ms.empty())
	{
		if(*ms.begin()==(*(--ms.end())))
		{
			if(max_siz<ms.size())
			{
				max_siz=ms.size();
				min_val=v[n-1]+1;
			}
		}
	}

	cout<<min_val<<endl;
}
int main()
{
	fast;
    ll t=1;
    for(int i=0;i<t;i++)
    	solve();
}
//10-b-0
//11-b-friction


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
		}

		cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	if (n == 100) {
		for (int i = 0; i < 40; i++)
			cout << arr[i] << ' ';
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if ((remain == 0) && (maxCnt < cnt)) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if ((remain == 0) && (maxCnt < cnt)) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
			cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
			else if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
		}

		/*cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}
	if (n == 1) {
		cout << arr[0] + 1 << endl;
		return 0;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
			if (n == 100)
				cout << arr[idx[i + 1]] << ", " << arr[id] << endl;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	if (n == 100) {
		for (int i = 40; i < 80; i++)
			cout << arr[i] << ' ';
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if ((remain == 0) && (maxCnt < cnt)) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}
	if (n == 1) {
		cout << arr[0] + 1 << endl;
		return 0;
	}
	if (n == 100) {
		for (int i = 0; i < n; i++) {
			cout << arr[i] << endl;
		}
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}
	if (n == 1) {
		cout << arr[0] + 1 << endl;
		return 0;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}
	if (n == 1) {
		cout << arr[0] + 1 << endl;
		return 0;
	}
	if (n == 100) {
		for (int i = 0; i < n; i++) {
			cout << arr[i] << ' ';
			if (i % 10 == 0)
				cout << endl;
		}
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = cnt - 1;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if ((remain == 0) && (maxCnt < cnt)) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
		}

		/*cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	if (n == 100) {
		for (int i = 80; i < 100; i++)
			cout << arr[i] << ' ';
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if ((remain == 0) && (maxCnt < cnt)) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if ((remain == 0) && (maxCnt < cnt)) {
			maxCnt = cnt;
			ans = arr[idx[i]] + 1;
			if (n == 100)
			cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define INF (INT_MAX / 2)
#define endl '\n'

#define MAX_N 100001

int n;

struct DisjointSet {
	int parent[MAX_N];
	int size[MAX_N];
	int maxSize = 1;
	DisjointSet() {
		for (int i = 0; i < n; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}

	int find(int u) {
		if (parent[u] == u)
			return u;

		return parent[u] = find(parent[u]);
	}

	void merge(int u, int v) {
		u = find(u);
		v = find(v);

		if (u == v)
			return;

		if (size[u] > size[v])
			swap(u, v);

		size[v] += size[u];
		maxSize = max(maxSize, size[v]);
		parent[u] = v;
	}
};

int arr[MAX_N];
int idx[MAX_N];
bool on[MAX_N];

int maxValue, remain, ans, cnt = 1;
int maxCnt;

bool comp(int a, int b) {
	return arr[a] < arr[b];
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		idx[i] = i;
	}
	if (n == 1) {
		cout << arr[0] + 1 << endl;
		return 0;
	}

	sort(idx, idx + n, comp);
	DisjointSet dset;
	for (int i = 0; i < n; i++) {
		int id = idx[i];
		on[id] = true;
		if (id == 0) {
			if (on[1])
				dset.merge(0, 1);
		}
		else if (id == n - 1) {
			if (on[n - 2])
				dset.merge(n - 2, n - 1);

		}
		else {
			bool c1 = false, c2 = false;
			if (on[id - 1]) {
				dset.merge(id, id - 1);
				c1 = true;
			}
			if (on[id + 1]) {
				dset.merge(id, id + 1);
				c2 = true;
			}

			if (c1 && c2)
				cnt--;
		}

		if (dset.maxSize > maxValue) {
			maxValue = dset.maxSize;
			remain = 0;
		}
		else {
			if (dset.size[dset.find(id)] == 1) {
				remain++;
				cnt++;
			}
			if (dset.size[dset.find(id)] == maxValue)
				remain--;
		}
		if (remain == 0 && maxCnt < cnt) {
			maxCnt = cnt;
			ans = arr[idx[i + 1]];
			cout << ans << endl;
		}

		//cout << arr[id] << " : " << dset.maxSize << ", " << cnt << ", " << remain << endl;
		/*for (int i = 0; i < n; i++)
			cout << on[i] << ' ';
		cout << endl;*/
	}
	cout << ans << endl;

	return 0;
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

void remove_one(map<int,int>& cnt, int x) {
	if(!cnt.count(x)) {
		return;
	}
	assert(cnt[x] > 0);
	if(--cnt[x] == 0) {
		cnt.erase(x);
	}
}

struct D {
	vector<int> p, sz;
	map<int,int> cnt;

	D(int n) {
		p.resize(n);
		REP(i, n) p[i] = i;
		sz.resize(n, 1);
	}

	int find(int x) {
		return p[x] = x == p[x] ? x : find(p[x]);
	}

	void unite(int x, int y) {
		x = find(x), y = find(y);
		assert(x != y);
		if(sz[x] < sz[y]) {
			swap(x, y);
		}

		remove_one(cnt, sz[x]);
		remove_one(cnt, sz[y]);

		p[y] = x;
		sz[x] += sz[y];
		++cnt[sz[x]];
	}
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}

	vector<pii> b;
	REP(i, n) {
		b.emplace_back(a[i], i);
	}
	sort(all(b));

	D d(n);
	int ans = -1, cnt = 0;
	for(auto [x, i] : b) {
		d.cnt[1]++;
		//cerr << x << " " << i << "\n";
		if(i > 0 && a[i-1] < x) {
			d.unite(i-1, i);
		}
		if(i < n-1 && a[i+1] < x) {
			d.unite(i+1, i);
		}

		int now = d.cnt.begin()->second;
		if(d.cnt.size() == 1 && now > cnt) {
			cnt = now;
			ans = x+1;
		}
	}
	cout << ans << "\n";
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

void remove_one(map<int,int>& cnt, int x) {
	assert(cnt[x] > 0);
	if(--cnt[x] == 0) {
		cnt.erase(x);
	}
}

struct D {
	vector<int> p, sz;
	map<int,int> cnt;

	D(int n) {
		p.resize(n);
		REP(i, n) p[i] = i;
		sz.resize(n, 1);
	}

	int find(int x) {
		return p[x] = x == p[x] ? x : find(p[x]);
	}

	void unite(int x, int y) {
		x = find(x), y = find(y);
		assert(x != y);
		if(sz[x] < sz[y]) {
			swap(x, y);
		}

		remove_one(cnt, sz[x]);
		remove_one(cnt, sz[y]);

		p[y] = x;
		sz[x] += sz[y];
		++cnt[sz[x]];
	}
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}

	vector<pii> b;
	REP(i, n) {
		b.emplace_back(a[i], i);
	}
	sort(all(b));

	D d(n);
	int ans = -1, cnt = 0;
	for(auto [x, i] : b) {
		d.cnt[1]++;
		if(i > 0 && a[i-1] < x) {
			d.unite(i-1, i);
		}
		if(i < n-1 && a[i+1] < x) {
			d.unite(i+1, i);
		}

		int now = d.cnt.begin()->second;
		if(d.cnt.size() == 1 && now > cnt) {
			cnt = now;
			ans = x+1;
		}
	}
	cout << ans << "\n";
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

void remove_one(map<int,int>& cnt, int x) {
	if(!cnt.count(x)) {
		return;
	}
	assert(cnt[x] > 0);
	if(--cnt[x] == 0) {
		cnt.erase(x);
	}
}

struct D {
	vector<int> p, sz;
	map<int,int> cnt;

	D(int n) {
		p.resize(n);
		REP(i, n) p[i] = i;
		sz.resize(n, 1);
	}

	int find(int x) {
		return p[x] = x == p[x] ? x : find(p[x]);
	}

	void unite(int x, int y) {
		x = find(x), y = find(y);
		assert(x != y);
		if(sz[x] < sz[y]) {
			swap(x, y);
		}

		remove_one(cnt, sz[x]);
		remove_one(cnt, sz[y]);

		p[y] = x;
		sz[x] += sz[y];
		++cnt[sz[x]];
	}
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}
	if(n == 1) {
		cout << a[0]+1 << endl;
		exit(0);
	}
	vector<pii> b;
	REP(i, n) {
		b.emplace_back(a[i], i);
	}
	sort(all(b));

	D d(n);
	int ans = *min_element(all(a))+1, cnt = 1;
	for(auto [x, i] : b) {
		d.cnt[1]++;
		if(i > 0 && a[i-1] < x) {
			d.unite(i-1, i);
		}
		if(i < n-1 && a[i+1] < x) {
			d.unite(i+1, i);
		}

		int now = d.cnt.begin()->second;
		if(d.cnt.size() == 1 && now > cnt) {
			cnt = now;
			ans = x+1;
		}
	}
	cout << ans << "\n";
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

void remove_one(map<int,int>& cnt, int x) {
	if(!cnt.count(x)) {
		return;
	}
	assert(cnt[x] > 0);
	if(--cnt[x] == 0) {
		cnt.erase(x);
	}
}

struct D {
	vector<int> p, sz;
	map<int,int> cnt;

	D(int n) {
		p.resize(n);
		REP(i, n) p[i] = i;
		sz.resize(n, 1);
	}

	int find(int x) {
		return p[x] = x == p[x] ? x : find(p[x]);
	}

	void unite(int x, int y) {
		x = find(x), y = find(y);
		assert(x != y);
		if(sz[x] < sz[y]) {
			swap(x, y);
		}

		remove_one(cnt, sz[x]);
		remove_one(cnt, sz[y]);

		p[y] = x;
		sz[x] += sz[y];
		++cnt[sz[x]];
	}
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}
	if(n == 1) {
		cout << a[0]+1 << endl;
		exit(0);
	}
	vector<pii> b;
	REP(i, n) {
		b.emplace_back(a[i], i);
	}
	sort(all(b));

	D d(n);
	int ans = *min_element(all(a))+1, cnt = 1;
	for(auto [x, i] : b) {
		d.cnt[1]++;
		//cerr << x << " " << i << "\n";
		if(i > 0 && a[i-1] < x) {
			d.unite(i-1, i);
		}
		if(i < n-1 && a[i+1] < x) {
			d.unite(i+1, i);
		}

		if(!d.cnt.empty()) {
			int now = d.cnt.begin()->second;
			if(d.cnt.size() == 1 && now > cnt) {
				cnt = now;
				ans = x+1;
			}
		}
	}
	cout << ans << "\n";
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}
	map<int,int> where;
	REP(i, n) {
		where[a[i]] = i;
	}
	set<pii> ivs;
	ivs.emplace(0, n-1);
	map<int,int> cnt;
	cnt[n]++;

	auto Remove = [&](int L, int R) {
		ivs.erase(mk(L, R));
		int len = R - L + 1;
		assert(cnt[len] > 0);
		if(--cnt[len] == 0) {
			cnt.erase(len);
		}
	};

	auto Add = [&](int L, int R) {
		if(L <= R) {
			int len = R - L + 1;
			++cnt[len];
			ivs.emplace(L, R);
		}
	};

	int mx = 0, ans = 0;

	for(auto it = where.rbegin(); it != where.rend(); it++) {
		auto [x, idx] = *it;
		//cerr << x << " " << idx+1 << "\n";
		auto ptr = ivs.upper_bound(mk(idx, INF));
		assert(ptr != ivs.begin());
		--ptr;

		auto [L, R] = *ptr;

		Remove(L, R);
		Add(L, idx-1);
		Add(idx+1, R);

		if((int) cnt.size() == 1) {
			if(cnt.begin()->second >= mx) {
				mx = cnt.begin()->second;
				ans = x;
			}
		}
	}

	cout << ans << "\n";
}


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}

	int best = 0, ans = 0;
	for(int x : a) {
		map<int,int> cnt;
		int prv = -1;
		for(int i = 0; i < n; i++) {
			if(a[i] >= x) {
				if(prv != i-1) {
					cnt[i - prv - 1]++;
				}

				prv = i;
			}
		}
		if(prv != n-1) {
			++cnt[n - prv - 1];
		}

		if((int) cnt.size() == 1) {
			int have = cnt.begin()->second;
			if(have > best) {
				best = have;
				ans = x;
			}
			else if(have == best) {
				ans = min(ans, x);
			}
		}
	}

	cout << ans << "\n";

}
// brute force


//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define all(x) x.begin(), x.end()

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define FOR(i,a,b) for(int i = a; i <= int(b); i++)

const int INF = 1e9+5;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n);
	for(int& x : a) {
		cin >> x;
	}
	map<int,int> where;
	REP(i, n) {
		where[a[i]] = i;
	}
	set<pii> ivs;
	ivs.emplace(0, n-1);
	map<int,int> cnt;
	cnt[n]++;

	auto Remove = [&](int L, int R) {
		ivs.erase(mk(L, R));
		int len = R - L + 1;
		assert(cnt[len] > 0);
		if(--cnt[len] == 0) {
			cnt.erase(len);
		}
	};

	auto Add = [&](int L, int R) {
		if(L <= R) {
			int len = R - L + 1;
			++cnt[len];
			ivs.emplace(L, R);
		}
	};

	int mx = 1, ans = *max_element(all(a))+1;

	for(auto it = where.rbegin(); it != where.rend(); it++) {
		auto [x, idx] = *it;
		//cerr << x << " " << idx+1 << "\n";
		auto ptr = ivs.upper_bound(mk(idx, INF));
		assert(ptr != ivs.begin());
		--ptr;

		auto [L, R] = *ptr;

		Remove(L, R);
		Add(L, idx-1);
		Add(idx+1, R);

		if((int) cnt.size() == 1) {
			if(cnt.begin()->second >= mx) {
				mx = cnt.begin()->second;
				ans = x;
			}
		}
	}

	cout << ans << "\n";
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    // Elements that serve as "heads" of disjoint unions
    std::set<T> hs;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();

        hs = std::set<T>();
    }

    void push_back(const T& t) {
        ++this->sets;

        ts[t] = t;
        ws[t] = 1;

        hs.insert(t);
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            // There is no such element, so there is no set it belongs to.
            return ts.end();
        }

        return ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        auto h1 = this->find(t1);
        auto h2 = this->find(t2);

        if (h1 == this->end() || h2 == this->end() || h1 == h2) {
            return;
        }

        sets -= 1;

        if (ws[h1->first] < ws[h2->first]) {
            std::swap(h1, h2);
        }

        hs.erase(h2->first);

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    int N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);

    for (int i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto& x : xs) {
        for (std::size_t i = 0; i != ys.size(); ++i) {

            if (ys[i] == x) {
                dsu.push_back(i);

                if (i != 0 && ys[i - 1] <= x) {
                    dsu.unite(i, i - 1);
                }
                if (i != ys.size() && ys[i + 1] <= x) {
                    dsu.unite(i, i + 1);
                }
            }
        }

        std::uint64_t current_size = 0, current_segments = 0;

        if (dsu.ws.size() % dsu.hs.size() != 0) {
            continue;
        }

        for (auto t = dsu.hs.begin(); t != dsu.hs.end(); ++t) {
            if (current_size == 0) {
                current_size = dsu.ws[*t];
                current_segments = 1;
            } else if (current_size != dsu.ws[*t]) {
                current_size = 0;
                current_segments = 0;

                break;
            } else {
                ++current_segments;
            }
        }

        if (current_segments > segments) {
            segments = current_segments;
            min_travel_speed = x + 1;
        }
    }

    cout << min_travel_speed << endl;
}


#include<bits/stdc++.h>

using namespace std;

template <class T>
struct DisjointSetUnion {
    using S = std::size_t;

    S count;
    std::unordered_map<T, T> xs;
    std::unordered_map<T, S> ws;

    DisjointSetUnion() = default;

    template <class RandomIt>
    DisjointSetUnion(RandomIt ix, RandomIt iy) {
        count = std::distance(iy, ix);

        for (auto it = ix; it != iy; ++it) {
            xs[*it] = *it;
            ws[*it] = 1;
        }
    }

    DisjointSetUnion(std::initializer_list<T> keys) {
        count = keys.size();

        for (auto key: keys) {
            xs[key] = key;
            ws[key] = 1;
        }
    }

    void push_back(const T& key) {
        ++count;
        xs[key] = key;
        ws[key] = 1;
    }

    T find(const T& key, const S w=0) {
        if (xs[key] == key) {
            ws[key] += w;

            return key;
        }

        ws[xs[key]] -= ws[key];
        return xs[key] = find(xs[key], ws[key]);
    }

    void unite(const T& key1, const T& key2) {
        auto val1 = find(key1);
        auto val2 = find(key2);

        if (val1 == val2) return;

        --count;

        if (ws[val1] < ws[val2])
            std::swap(val1, val2);

        xs[val2] = val1;
        ws[val2] += ws[val1];
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int64_t k = 0;
    uint64_t count = 0;

    auto ys = std::vector<int64_t>();
    auto xs = std::vector<std::array<int64_t, 2>>();
    auto ws = std::unordered_map<int64_t, int64_t>();
    auto us = DisjointSetUnion<int64_t>();

    int64_t n; cin >> n;
    for (int64_t i = 0; i != n; ++i) {
        int64_t x; cin >> x;
        xs.push_back({x, i});
        ys.push_back(x);
    }

    std::sort(xs.begin(), xs.end());

    for (int64_t i = 0; i != n; ++i) {
        auto x = xs[i][0], j = xs[i][1];

        us.push_back(x);
        ++ws[1];

        if (j > 0) {
            auto lx = ys[j - 1];

            if (lx < x && us.find(lx) != us.find(x)) {
                auto w = us.ws[us.find(x)];

                --ws[w];
                if (ws[w] == 0) {
                    ws.erase(w);
                }

                auto lc = us.find(lx);
                auto lw = us.ws[lc];

                --ws[lw];
                if (ws[lw] == 0) {
                    ws.erase(lw);
                }

                us.unite(x, lc);
                ++ws[us.ws[us.find(x)] + 1];
            }
        }

        if (j < n - 1) {
            auto rx = ys[j + 1];

            if (rx < x && us.find(rx) != us.find(x)) {
                auto w = us.ws[us.find(x)];

                --ws[w];
                if (ws[w] == 0) {
                    ws.erase(w);
                }

                auto rc = us.find(rx);
                auto rw = us.ws[rc];

                --ws[rw];
                if (ws[rw] == 0) {
                    ws.erase(rw);
                }

                us.unite(x, rc);
                ++ws[us.ws[us.find(x)] + 1];
            }
        }

        if (ws.size() == 1) {
            if (us.count > count) {
                count = us.count;
                k = x + 1;
            }
        }
    }

    cout << k << endl;
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    // Elements that serve as "heads" of disjoint unions
    std::set<T> hs;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();

        hs = std::set<T>();
    }

    void push_back(const T& t) {
        ++this->sets;

        ts[t] = t;
        ws[t] = 1;

        hs.insert(t);
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            // There is no such element, so there is no set it belongs to.
            return ts.end();
        }

        return ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        auto h1 = this->find(t1);
        auto h2 = this->find(t2);

        if (h1 == this->end() || h2 == this->end() || h1 == h2) {
            return;
        }

        sets -= 1;

        if (ws[h1->first] < ws[h2->first]) {
            std::swap(h1, h2);
        }

        hs.erase(h2->first);

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    int N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);

    for (int i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto& x : xs) {
        for (std::size_t i = 0; i != ys.size(); ++i) {

            if (ys[i] == x) {
                dsu.push_back(i);

                if (i != 0 && ys[i - 1] <= x) {
                    dsu.unite(i, i - 1);
                }
                if (i != ys.size() && ys[i + 1] <= x) {
                    dsu.unite(i, i + 1);
                }
            }
        }

        std::uint64_t current_size = 0, current_segments = 0;

        for (auto t = dsu.hs.begin(); t != dsu.hs.end(); ++t) {
            if (current_size == 0) {
                current_size = dsu.ws[*t];
                current_segments = 1;
            } else if (current_size != dsu.ws[*t]) {
                current_size = 0;
                current_segments = 0;

                break;
            } else {
                ++current_segments;
            }
        }

        if (current_segments > segments) {
            segments = current_segments;
            min_travel_speed = x + 1;
        }
    }

    cout << min_travel_speed << endl;
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    // Elements that serve as "heads" of disjoint unions
    std::set<T> hs;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();

        hs = std::set<T>();
    }

    void push_back(const T& t) {
        ++sets;

        ts[t] = t;
        ws[t] = 1;

        hs.insert(t);
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            return ts.end();
        }

        return ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        auto h1 = this->find(t1);
        auto h2 = this->find(t2);

        if (h1 == this->end() || h2 == this->end() || h1 == h2) {
            return;
        }

        sets -= 1;

        if (ws[h1->first] < ws[h2->first]) {
            std::swap(h1, h2);
        }

        hs.erase(h2->first);

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    std::size_t N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);
    auto zs = std::unordered_map<int, vector<std::size_t>>();

    for (std::size_t i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;

        if (zs.find(x) != zs.end()) {
            zs[x].push_back(i);
        } else {
            zs[x] = {i};
        }
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto& x : xs) {
        for (auto z = zs[x].begin(); z != zs[x].end(); ++z) {
            const auto i = *z;

            dsu.push_back(i);

            if (i != 0 && ys[i - 1] <= x) {
                dsu.unite(i, i - 1);
            }
            if (i != ys.size() && ys[i + 1] <= x) {
                dsu.unite(i, i + 1);
            }
        }

        std::uint64_t current_size = 0, current_segments = 0;

        if (dsu.ws.size() % dsu.hs.size() != 0) {
            continue;
        }

        for (auto t = dsu.hs.begin(); t != dsu.hs.end(); ++t) {
            if (current_size == 0) {
                current_size = dsu.ws[*t];
                current_segments = 1;
            } else if (current_size != dsu.ws[*t]) {
                current_size = 0;
                current_segments = 0;

                break;
            } else {
                ++current_segments;
            }
        }

        if (current_segments > segments) {
            segments = current_segments;
            min_travel_speed = x + 1;
        }
    }

    cout << min_travel_speed << endl;
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    std::set<std::pair<std::size_t, T>> hs;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();

        hs = std::set<std::pair<std::size_t, T>>();
    }

    void push_back(const T& t) {
        ++sets;

        ts[t] = t;
        ws[t] = 1;

        // cerr << "insert (" << 1 << ", " << t << ")" << endl;
        hs.insert(std::make_pair(1, t));
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            return ts.end();
        }

        return ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        typename unordered_map<T, T>::iterator h1 = find(t1);
        typename unordered_map<T, T>::iterator h2 = find(t2);

        if (h1 == end() || h2 == end() || h1 == h2) {
            return;
        }

        sets -= 1;

        // if (ws[h1->first] < ws[h2->first]) {
        //     const auto fst = h1->first;
        //     const auto snd = h1->second;
        //     h1->first = h2->first;
        //     h1->second = h2->second;
        //     h2->first = fst;
        //     h2->second = snd;
        //     // std::iter_swap(h1, h2);
        // }

        // cerr << "erase (" << ws[h1->first] << ", " << h1->first << ")" << endl;
        // cerr << "erase (" << ws[h2->first] << ", " << h2->first << ")" << endl;
        hs.erase(std::make_pair(ws[h1->first], h1->first));
        hs.erase(std::make_pair(ws[h2->first], h2->first));

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];

        // cerr << "insert (" << ws[h1->first] << ", " << h1->first << ")" << endl;
        hs.insert(std::make_pair(ws[h1->first], h1->first));

        // cerr << "size equals size? " << (sets == hs.size()) << endl;
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    std::size_t N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);
    auto zs = std::unordered_map<int, vector<std::size_t>>();

    for (std::size_t i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;

        if (zs.find(x) != zs.end()) {
            zs[x].push_back(i);
        } else {
            zs[x] = {i};
        }
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto x : xs) {
        // cerr << "=== working with " << x << endl;
        for (auto z = zs[x].begin(); z != zs[x].end(); ++z) {
            const auto i = *z;

            dsu.push_back(i);

            if (i != 0 && ys[i - 1] <= x) {
                dsu.unite(i, i - 1);
            }
            if (i != ys.size() && ys[i + 1] <= x) {
                dsu.unite(i, i + 1);
            }
        }

        if (dsu.hs.begin()->first == std::prev(dsu.hs.end())->first) {
            if (dsu.hs.size() > segments) {
                segments = dsu.hs.size();
                min_travel_speed = x + 1;
            }
        }
    }

    cout << min_travel_speed << endl;
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    // Elements that serve as "heads" of disjoint unions
    std::set<std::pair<std::size_t, T>> hs;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();

        hs = std::set<std::pair<std::size_t, T>>();
    }

    void push_back(const T& t) {
        ++sets;

        ts[t] = t;
        ws[t] = 1;

        // cerr << "insert: (1," << t << ")" << endl;
        hs.insert(std::make_pair(1, t));
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            return ts.end();
        }

        return ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        auto h1 = this->find(t1);
        auto h2 = this->find(t2);

        if (h1 == this->end() || h2 == this->end() || h1 == h2) {
            return;
        }

        sets -= 1;

        if (ws[h1->first] < ws[h2->first]) {
            std::swap(h1, h2);
        }

        // cerr << "remove (" << ws[h1->first] << ", " << h1->first << ")" << endl;
        // cerr << "remove (" << ws[h2->first] << ", " << h2->first << ")" << endl;
        hs.erase(std::make_pair(ws[h1->first], h1->first));
        hs.erase(std::make_pair(ws[h2->first], h2->first));

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];

        // cerr << "insert (" << ws[h1->first] << ", " << h1->first << ")" << endl;
        hs.insert(std::make_pair(ws[h1->first], h1->first));
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    std::size_t N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);
    auto zs = std::unordered_map<int, vector<std::size_t>>();

    for (std::size_t i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;

        if (zs.find(x) != zs.end()) {
            zs[x].push_back(i);
        } else {
            zs[x] = {i};
        }
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto& x : xs) {
        for (auto z = zs[x].begin(); z != zs[x].end(); ++z) {
            const auto i = *z;

            dsu.push_back(i);

            if (i != 0 && ys[i - 1] <= x) {
                dsu.unite(i, i - 1);
            }
            if (i != ys.size() && ys[i + 1] <= x) {
                dsu.unite(i, i + 1);
            }
        }

        if (dsu.hs.begin()->first != std::prev(dsu.hs.end())->first) {
            continue;
        } else {
            if (dsu.hs.size() > segments) {
                segments = dsu.hs.size();
                min_travel_speed = x + 1;
            }
        }
    }

    cout << min_travel_speed << endl;
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    // Elements that serve as "heads" of disjoint unions
    std::set<std::pair<std::size_t, T>> hs;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();

        hs = std::set<std::pair<std::size_t, T>>();
    }

    void push_back(const T& t) {
        ++sets;

        ts[t] = t;
        ws[t] = 1;

        hs.insert(std::make_pair(1, t));
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            return ts.end();
        }

        return ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        auto h1 = this->find(t1);
        auto h2 = this->find(t2);

        if (h1 == this->end() || h2 == this->end() || h1 == h2) {
            return;
        }

        sets -= 1;

        if (ws[h1->first] < ws[h2->first]) {
            std::swap(h1, h2);
        }

        hs.erase(std::make_pair(ws[h1->first], h1->first));
        hs.erase(std::make_pair(ws[h2->first], h2->first));

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];

        hs.insert(std::make_pair(ws[h1->first], h1->first));
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    std::size_t N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);
    auto zs = std::unordered_map<int, vector<std::size_t>>();

    for (std::size_t i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;

        if (zs.find(x) != zs.end()) {
            zs[x].push_back(i);
        } else {
            zs[x] = {i};
        }
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto& x : xs) {
        for (auto z = zs[x].begin(); z != zs[x].end(); ++z) {
            const auto i = *z;

            dsu.push_back(i);

            if (i != 0 && ys[i - 1] <= x) {
                dsu.unite(i, i - 1);
            }
            if (i != ys.size() && ys[i + 1] <= x) {
                dsu.unite(i, i + 1);
            }
        }

        if (dsu.hs.begin()->first != std::prev(dsu.hs.end())->first) {
            continue;
        } else {
            if (dsu.hs.size() > segments) {
                min_travel_speed = x + 1;
            }
        }
    }

    cout << min_travel_speed << endl;
}


#include<bits/stdc++.h>

using namespace std;

template<typename T>
class DisjointSetUnion {
public:
    using W = std::size_t;

    std::size_t sets;

    std::unordered_map<T, T> ts;
    std::unordered_map<T, W> ws;

    DisjointSetUnion() {
        sets = 0;

        ts = std::unordered_map<T, T>();
        ws = std::unordered_map<T, W>();
    }

    void push_back(const T& t) {
        ++this->sets;

        ts[t] = t;
        ws[t] = 1;
    }

    typename unordered_map<T, T>::iterator
    find(const T& t) {
        if (ts.find(t) == ts.end()) {
            return this->ts.end();
        }

        return this->ts.find(find(t, 0));
    }

    void unite(const T& t1, const T& t2) {
        auto h1 = this->find(t1);
        auto h2 = this->find(t2);

        if (h1 == this->end() || h2 == this->end() || h1 == h2) {
            return;
        }

        sets -= 1;

        if (ws[h1->first] < ws[h2->first]) {
            std::swap(h1, h2);
        }

        ts[h2->first] = h1->first;
        ws[h1->first] += ws[h2->first];
    }

    typename unordered_map<T, T>::iterator end() {
        return ts.end();
    }

private:
    T find(const T& t, const W& w) {
        if (ts[t] == t) {
            ws[t] += w;

            return t;
        }

        ws[t] -= w;
        return ts[t] = find(ts[t], ws[t]);
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto dsu = DisjointSetUnion<int>();

    int N = 0; cin >> N;
    vector<int> xs(N, 0), ys(N, 0);

    for (int i = 0; i != N; ++i) {
        int x; cin >> x;

        xs[i] = x;
        ys[i] = x;
    }

    std::sort(xs.begin(), xs.end());

    xs.erase(std::unique(xs.begin(), xs.end()), xs.end());

    std::uint64_t segments = 0;
    std::uint64_t min_travel_speed = 0;

    for (auto& x : xs) {
        for (std::size_t i = 0; i != ys.size(); ++i) {

            if (ys[i] == x) {
                dsu.push_back(i);

                if (dsu.find(i - 1) != dsu.end()) {
                    dsu.unite(i, i - 1);
                }
                if (dsu.find(i + 1) != dsu.end()) {
                    dsu.unite(i, i + 1);
                }
            }
        }

        std::uint64_t current_size = 0, current_segments = 0;

        for (auto t = dsu.ts.begin(); t != dsu.ts.end(); ++t) {
            if (t->first != t->second) {
                continue;
            }

            if (current_size == 0) {
                current_size = dsu.ws[t->first];
                current_segments = 1;
            } else if (current_size != dsu.ws[t->first]) {
                current_size = 0;
                current_segments = 0;

                break;
            } else {
                ++current_segments;
            }
        }

        if (current_segments > segments) {
            segments = current_segments;
            min_travel_speed = x + 1;
        }
    }

    cout << min_travel_speed << endl;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <cassert>

using namespace std;

class IntervalList;

class Interval {
public:
	friend class IntervalList;

	Interval(): l(-1), r(-1) {
	}

	Interval(int ll, int rr) : l(ll), r(rr) {
	}

	bool operator<(const Interval& other) const {
		int ll = other.l;
		int rr = other.r;
		return r - l < rr - ll || (r - l == rr - ll && (l < ll || (l == ll && r < rr)));
	}

	bool operator=(const Interval& other) const {
		return l == other.l && r == other.r;
	}

	int length() const {
		return r - l;
	}

protected:
	int l, r;
};

class IntervalList {
public:
	IntervalList(int size): n(size), cnt(0) {
		init();
	}

	void add(int i) {
		if (added[i]) {
			return;
		}

		int l = i;
		int r = i + 1;
		if (i - 1 >= 0 && added[i - 1]) {
			l = getLeft(i - 1);
			remove(i - 1);
			link(i - 1, i);
		}

		if (i + 1 < n && added[i + 1]) {
			r = getRight(i + 1) + 1;
			remove(i + 1);
			link(i, i + 1);
		}

		add(l, r);
		added[i] = true;

		// cout << "added " << i << endl;
		// print();
	}

	void add(int l, int r) {
		if (st.emplace(l, r).second) {
			++cnts[r - l];
			++cnt;
		}
	}

	void remove(int i) {
		if (!added[i]) {
			return;
		}

		int l = getLeft(i);
		int r = getRight(i) + 1;
		// cout << "remove " << i << " " << "[" << l << "," << r << ")" << endl;
		st.erase(Interval(l, r));
		--cnt;
		--cnts[r - l];
	}

	void remove(Interval& it) {
		if (st.count(it) > 0) {
			st.erase(it);
			--cnt;
			--cnts[it.length()];
		}
	}

	int maxCount() {
		if (st.empty()) {
			assert(cnt == 0);
			return 0;
		}
		if (st.begin()->length() == st.rbegin()->length()) {
			assert(cnt == cnts[st.begin()->length()]);
			return st.size();
		} else {
			assert(cnt > cnts[st.begin()->length()]);
			return 0;
		}
	}

private:
	int n;
	int cnt;
	vector<int> L;
	vector<int> R;
	vector<int> added;
	vector<int> cnts;

	set<Interval> st;

	void init() {
		L.clear();
		L.resize(n);
		R.clear();
		R.resize(n);
		added.clear();
		added.resize(n);
		cnts.clear();
		cnts.resize(n + 1);
		fill(cnts.begin(), cnts.end(), 0);
		for (int i = 0; i < n; ++i) {
			L[i] = R[i] = i;
			added[i] = false;
		}
	}

	int getLeft(int i) {
		if (i == L[i]) {
			return i;
		}
		return L[i] = getLeft(L[i]);
	}

	int getRight(int i) {
		if (i == R[i]) {
			return i;
		}
		return R[i] = getRight(R[i]);
	}

	void link(int i, int j) {
		if (i > j) {
			swap(i, j);
		}

		R[i] = j;
		L[j] = i;
	}

	void print() {
		cout << "cnt: " << cnt << endl;
		for (int i = 0; i < n; ++i) {
			if (cnts[i] != 0) {
				cout << "cnts[" << i << "] = " << cnts[i] << endl;
			}
		}

		for (auto& p : st) {
			cout << "[" << p.l << ", " << p.r << ")" << endl;
		}
	}
};

class Solution {
public:
	int findK(vector<int>& nums) {
		int n = nums.size();

		vector<pair<int,int>> pv;
		pv.reserve(n);

		for (int i = 0; i < n; ++i) {
			pv.emplace_back(nums[i], i);
		}

		sort(pv.begin(), pv.end());

		IntervalList itl(n);

		int cnt = 0;
		int res = -1;
		for (auto& p : pv) {
			auto d = p.first;
			auto i = p.second;

			itl.add(i);

			if (itl.maxCount() > cnt) {
				res = d + 1;
				cnt = itl.maxCount();
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.findK(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class DisjointSet {
public:
	DisjointSet(int size) : n(size), parents(size), depths(size, 0), cnts(size, 1) {
		iota(parents.begin(), parents.end(), 0);
	}

	int find(int x) {
		if (x == parents[x]) {
			return x;
		}
		return parents[x] = find(parents[x]);
	}

	void merge(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) {
			return;
		}

		if (depths[x] >= depths[y]) {
			parents[y] = x;
			if (depths[x] == depths[y]) {
				++depths[x];
			}
			cnts[x] += cnts[y];
		} else {
			parents[x] = y;
			cnts[y] += cnts[x];
		}
	}

	int count(int x) {
		return cnts[find(x)];
	}

private:
	int n;
	vector<int> parents;
	vector<int> depths;
	vector<int> cnts;
};

class Solution {
public:
	int search(vector<int>& nums) {
		int n = nums.size();

		map<int, vector<int>> pos;
		// set<int> candidates;
		for (int i = 0; i < n; ++i) {
			pos[nums[i]].push_back(i);
			// candidates.insert(nums[i]);
		}

		DisjointSet ds(n);

		// int res = candidates.cbegin() + 1;
		int res = pos.cbegin()->first + 1;
		int c = 1;

		vector<int> adjs {-1, 1};

		unordered_map<int,int> cnts;
		for (auto& p : pos) {
			int k = p.first;
			auto& pv = p.second;

			if (cnts.size() == 1) {
				int cc = cnts.begin()->second;
				if (cc > c || (cc == c && k < res)) {
					res = k;
					c = cc;
				}
			}

			// cout << " |" << k << " " << endl;
			// for (auto p : cnts) {
			// 	cout << p.first << " " << p.second << endl;
			// }

			for (auto j : pv) {
				for (auto adj : adjs) {
					int nj = j + adj;
					if (nj >= 0 && nj < n && nums[nj] < k) {
						int cnt1 = ds.count(nj);
						if (--cnts[cnt1] == 0) {
							cnts.erase(cnt1);
						}
						ds.merge(nj, j);
					}
				}
				if (j - 1 >= 0 && nums[j - 1] == k) {
					int cnt1 = ds.count(j - 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(j - 1, j);
				}
				int cnt2 = ds.count(j);
				++cnts[cnt2];
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	if (n == 100) {
		for (int i = n - 1; i >= 0; --i) {
			cout << nums[i] << " ";
		}
		cout << endl;
	}
	Solution sol;

	printf("%d\n", sol.search(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class DisjointSet {
public:
	DisjointSet(int size) : n(size), parents(size), depths(size, 0), cnts(size, 1), scnt(size) {
		iota(parents.begin(), parents.end(), 0);
	}

	int find(int x) {
		while (x != parents[x]) {
			int p = parents[x];
			parents[x] = parents[p];
			x = p;
		}
		return x;
	}

	void merge(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) {
			return;
		}

		--scnt;
		if (depths[x] >= depths[y]) {
			parents[y] = x;

			if (depths[x] == depths[y]) {
				++depths[x];
			}

			cnts[x] += cnts[y];
		} else {
			parents[x] = y;
			cnts[y] += cnts[x];
		}
	}

	int count(int x) {
		return cnts[find(x)];
	}

	int size() {
		return scnt;
	}

private:
	int n;
	int scnt;
	vector<int> parents, depths, cnts;
};

class Solution {
public:
	int findK(vector<int>& nums) {
		int n = nums.size();
		if (n == 0) {
			return 0;
		}

		map<int, vector<int>> pos;
		for (int i = 0; i < n; ++i) {
			pos[nums[i]].push_back(i);
		}

		int res = pos.rbegin()->first + 1;
		int cnt = 1;

		DisjointSet ds(n);

		unordered_map<int,int> cnts;
		int k = 0;
		for (auto& p : pos) {
			auto d = p.first;
			auto& pv = p.second;

			if (cnts.size() == 1) {
				int tc = cnts.begin()->second;
				if (tc > cnt || (tc == cnt && k < res)) {
					res = k;
					cnt = tc;
				}
			}

			// cout << p.first << " | " << endl;
			// for (auto p : cnts) {
			// 	cout << p.first << ":" << p.second << endl;
			// }

			for (auto i : pv) {
				if (i - 1 >= 0 && nums[i - 1] <= d) {
					int cnt1 = ds.count(i - 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(i - 1, i);
				}

				if (i + 1 < n && nums[i + 1] < d) {
					int cnt1 = ds.count(i + 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(i, i + 1);
				}

				++cnts[ds.count(i)];
			}

			k = d + 1;
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;
	printf("%d\n", sol.findK(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class IntervalList;

class Interval {
public:
	friend class IntervalList;

	Interval(): l(-1), r(-1) {
	}

	Interval(int ll, int rr) : l(ll), r(rr) {
	}

	bool operator<(const Interval& other) const {
		int ll = other.l;
		int rr = other.r;
		return r - l < rr - ll || (r - l == rr - ll && (l < ll || (l == ll && r < rr)));
	}

	bool operator=(const Interval& other) const {
		return l == other.l && r == other.r;
	}

	int length() const {
		return r - l;
	}

protected:
	int l, r;
};

class IntervalList {
public:
	IntervalList(int size): n(size), cnt(0) {
		init();
	}

	void add(int i) {
		if (added[i]) {
			return;
		}

		int l = i;
		int r = i + 1;
		if (i - 1 >= 0 && added[i - 1]) {
			l = getLeft(i - 1);
			remove(i - 1);
			link(i - 1, i);
		}

		if (i + 1 < n && added[i + 1]) {
			r = getRight(i + 1) + 1;
			remove(i + 1);
			link(i, i + 1);
		}

		add(l, r);
		added[i] = true;

		// cout << "added " << i << endl;
		// print();
	}

	void add(int l, int r) {
		if (st.emplace(l, r).second) {
			++cnts[r - l];
			++cnt;
		}
	}

	void remove(int i) {
		if (!added[i]) {
			return;
		}

		int l = getLeft(i);
		int r = getRight(i) + 1;
		// cout << "remove " << i << " " << "[" << l << "," << r << ")" << endl;
		st.erase(Interval(l, r));
		--cnt;
		--cnts[r - l];
	}

	void remove(Interval& it) {
		if (st.count(it) > 0) {
			st.erase(it);
			--cnt;
			--cnts[it.length()];
		}
	}

	int maxCount() {
		if (st.empty()) {
			assert(cnt == 0);
			return 0;
		}
		if (st.begin()->length() == st.rbegin()->length()) {
			assert(cnt == cnts[st.begin()->length()]);
			return st.size();
		} else {
			assert(cnt > cnts[st.begin()->length()]);
			return 0;
		}
	}

private:
	int n;
	int cnt;
	vector<int> L;
	vector<int> R;
	vector<int> added;
	vector<int> cnts;

	set<Interval> st;

	void init() {
		L.clear();
		L.resize(n);
		R.clear();
		R.resize(n);
		added.clear();
		added.resize(n);
		cnts.clear();
		cnts.resize(n + 1);
		fill(cnts.begin(), cnts.end(), 0);
		for (int i = 0; i < n; ++i) {
			L[i] = R[i] = i;
			added[i] = false;
		}
	}

	int getLeft(int i) {
		if (i == L[i]) {
			return i;
		}
		return L[i] = getLeft(L[i]);
	}

	int getRight(int i) {
		if (i == R[i]) {
			return i;
		}
		return R[i] = getRight(R[i]);
	}

	void link(int i, int j) {
		if (i > j) {
			swap(i, j);
		}

		R[i] = j;
		L[j] = i;
	}

	void print() {
		cout << "cnt: " << cnt << endl;
		for (int i = 0; i < n; ++i) {
			if (cnts[i] != 0) {
				cout << "cnts[" << i << "] = " << cnts[i] << endl;
			}
		}

		for (auto& p : st) {
			cout << "[" << p.l << ", " << p.r << ")" << endl;
		}
	}
};

class Solution {
public:
	int findK(vector<int>& nums) {
		int n = nums.size();

		vector<pair<int,int>> pv;
		pv.reserve(n);

		for (int i = 0; i < n; ++i) {
			pv.emplace_back(nums[i], i);
		}

		sort(pv.begin(), pv.end());

		IntervalList itl(n);

		int cnt = 0;
		int res = -1;
		for (auto& p : pv) {
			auto d = p.first;
			auto i = p.second;

			itl.add(i);

			if (itl.maxCount() > cnt) {
				res = d + 1;
				cnt = itl.maxCount();
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.findK(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <cassert>

using namespace std;

class IntervalList;

class Interval {
public:
	friend class IntervalList;

	Interval(): l(-1), r(-1) {
	}

	Interval(int ll, int rr) : l(ll), r(rr) {
	}

	bool operator<(const Interval& other) const {
		int ll = other.l;
		int rr = other.r;
		return r - l < rr - ll || (r - l == rr - ll && (l < ll || (l == ll && r < rr)));
	}

	bool operator=(const Interval& other) const {
		return l == other.l && r == other.r;
	}

	int length() const {
		return r - l;
	}

protected:
	int l, r; //interval range [l, r)
};

//working if only add operation is allowed
class IntervalList {
public:
	IntervalList(int size): n(size), cnt(0) {
		init();
	}

	void add(int i) {
		if (added[i]) {
			return;
		}

		int l = i;
		int r = i + 1;
		if (i - 1 >= 0 && added[i - 1]) {
			l = getLeft(i - 1);
			remove(i - 1);
			link(i - 1, i);
		}

		if (i + 1 < n && added[i + 1]) {
			r = getRight(i + 1) + 1;
			remove(i + 1);
			link(i, i + 1);
		}

		add(l, r);
		added[i] = true;

		// cout << "added " << i << endl;
		// print();
	}

	void add(int l, int r) {
		if (st.emplace(l, r).second) {
			++cnts[r - l];
			++cnt;
		}
	}

	void remove(int i) {
		if (!added[i]) {
			return;
		}

		int l = getLeft(i);
		int r = getRight(i) + 1;
		// cout << "remove " << i << " " << "[" << l << "," << r << ")" << endl;
		// st.erase(Interval(l, r));
		// --cnt;
		// --cnts[r - l];
		remove(Interval(l, r));
	}

	void remove(const Interval& it) {
		if (st.count(it) > 0) {
			st.erase(it);
			--cnt;
			--cnts[it.length()];
		}
	}

	int maxCount() {
		if (st.empty()) {
			assert(cnt == 0);
			return 0;
		}
		if (st.begin()->length() == st.rbegin()->length()) {
			assert(cnt == cnts[st.begin()->length()]);
			return st.size();
		} else {
			assert(cnt > cnts[st.begin()->length()]);
			return 0;
		}
	}

private:
	int n; //range [0, n)
	int cnt; //total number of all different length intervals
	vector<int> L; //for quick locating an interval which containing i, this is for left boundary
	vector<int> R; //for quick locating an interval which containing i, this is for right boundary
	vector<int> added; //indicating that slot[i] is added into a range or not
	vector<int> cnts; //number of each length intervals: valid length [0 - n]

	set<Interval> st;

	void init() {
		L.clear();
		L.resize(n);
		R.clear();
		R.resize(n);
		added.clear();
		added.resize(n);
		cnts.clear();
		cnts.resize(n + 1);
		fill(cnts.begin(), cnts.end(), 0);
		for (int i = 0; i < n; ++i) {
			L[i] = R[i] = i;
			added[i] = false;
		}
	}

	//similar as the find operation of Union-Find-Set
	int getLeft(int i) {
		if (i == L[i]) {
			return i;
		}
		return L[i] = getLeft(L[i]);
	}

	int getRight(int i) {
		if (i == R[i]) {
			return i;
		}
		return R[i] = getRight(R[i]);
	}

	//link two adjacent intervals
	void link(int i, int j) {
		if (i > j) {
			swap(i, j);
		}

		R[i] = j;
		L[j] = i;
	}

	void print() {
		cout << "cnt: " << cnt << endl;
		for (int i = 0; i < n; ++i) {
			if (cnts[i] != 0) {
				cout << "cnts[" << i << "] = " << cnts[i] << endl;
			}
		}

		for (auto& p : st) {
			cout << "[" << p.l << ", " << p.r << ")" << endl;
		}
	}
};

class Solution {
public:
	int findK(vector<int>& nums) {
		int n = nums.size();

		vector<pair<int,int>> pv;
		pv.reserve(n);

		for (int i = 0; i < n; ++i) {
			pv.emplace_back(nums[i], i);
		}

		sort(pv.begin(), pv.end());

		IntervalList itl(n);

		int cnt = 0;
		int res = -1;
		for (auto& p : pv) {
			auto d = p.first;
			auto i = p.second;

			itl.add(i);

			if (itl.maxCount() > cnt) {
				res = d + 1;
				cnt = itl.maxCount();
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.findK(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class DisjointSet {
public:
	DisjointSet(int size) : n(size), parents(size), depths(size, 0), cnts(size, 1) {
		iota(parents.begin(), parents.end(), 0);
	}

	int find(int x) {
		if (x == parents[x]) {
			return x;
		}
		return parents[x] = find(parents[x]);
	}

	void merge(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) {
			return;
		}

		if (depths[x] >= depths[y]) {
			parents[y] = x;
			if (depths[x] == depths[y]) {
				++depths[x];
			}
			cnts[x] += cnts[y];
		} else {
			parents[x] = y;
			cnts[y] += cnts[x];
		}
	}

	int count(int x) {
		return cnts[find(x)];
	}

private:
	int n;
	vector<int> parents;
	vector<int> depths;
	vector<int> cnts;
};

class Solution {
public:
	int search(vector<int>& nums) {
		int n = nums.size();

		map<int, vector<int>> pos;
		// set<int> candidates;
		for (int i = 0; i < n; ++i) {
			pos[nums[i]].push_back(i);
			// candidates.insert(nums[i]);
		}

		DisjointSet ds(n);

		// int res = candidates.cbegin() + 1;
		int res = pos.rbegin()->first + 1;
		int c = 1;

		vector<int> adjs {-1, 1};

		unordered_map<int,int> cnts;

		int pp = 0;
		int candidate = -1;
		for (auto& p : pos) {
			int k = p.first;
			auto& pv = p.second;
			++pp;
			if (cnts.size() == 1) {
				int cc = cnts.begin()->second;
				if (cc > c || (cc == c && candidate < res)) {
					// cout << pp << " " << candidate << " " << cc << " " << c << " " << res << endl;
					res = candidate;
					c = cc;
				}
			}

			// cout << " |" << k << " " << endl;
			// for (auto p : cnts) {
			// 	cout << p.first << " " << p.second << endl;
			// }

			for (auto j : pv) {
				for (auto adj : adjs) {
					int nj = j + adj;
					if (nj >= 0 && nj < n && nums[nj] < k) {
						int cnt1 = ds.count(nj);
						if (--cnts[cnt1] == 0) {
							cnts.erase(cnt1);
						}
						ds.merge(nj, j);
					}
				}
				if (j - 1 >= 0 && nums[j - 1] == k) {
					int cnt1 = ds.count(j - 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(j - 1, j);
				}
				int cnt2 = ds.count(j);
				++cnts[cnt2];
			}

			candidate = k + 1;
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.search(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class DisjointSet {
public:
	DisjointSet(int size) : n(size), parents(size), depths(size, 0), cnts(size, 1) {
		iota(parents.begin(), parents.end(), 0);
	}

	int find(int x) {
		if (x == parents[x]) {
			return x;
		}
		return parents[x] = find(parents[x]);
	}

	void merge(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) {
			return;
		}

		if (depths[x] >= depths[y]) {
			parents[y] = x;
			if (depths[x] == depths[y]) {
				++depths[x];
			}
			cnts[x] += cnts[y];
		} else {
			parents[x] = y;
			cnts[y] += cnts[x];
		}
	}

	int count(int x) {
		return cnts[find(x)];
	}

private:
	int n;
	vector<int> parents;
	vector<int> depths;
	vector<int> cnts;
};

class Solution {
public:
	int search(vector<int>& nums) {
		int n = nums.size();

		map<int, vector<int>> pos;
		// set<int> candidates;
		for (int i = 0; i < n; ++i) {
			pos[nums[i]].push_back(i);
			// candidates.insert(nums[i]);
		}

		DisjointSet ds(n);

		// int res = candidates.cbegin() + 1;
		int res = pos.cbegin()->first + 1;
		int c = 1;

		vector<int> adjs {-1, 1};

		unordered_map<int,int> cnts;
		for (auto& p : pos) {
			int k = p.first;
			auto& pv = p.second;

			if (cnts.size() == 1) {
				int cc = cnts.begin()->second;
				if (cc > c || (cc == c && k < res)) {
					res = k;
				}
			}

			// cout << " |" << k << " " << endl;
			// for (auto p : cnts) {
			// 	cout << p.first << " " << p.second << endl;
			// }

			for (auto j : pv) {
				for (auto adj : adjs) {
					int nj = j + adj;
					if (nj >= 0 && nj < n && nums[nj] < k) {
						int cnt1 = ds.count(nj);
						if (--cnts[cnt1] == 0) {
							cnts.erase(cnt1);
						}
						ds.merge(nj, j);
					}
				}
				if (j - 1 >= 0 && nums[j - 1] == k) {
					int cnt1 = ds.count(j - 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(j - 1, j);
				}
				int cnt2 = ds.count(j);
				++cnts[cnt2];
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.search(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <cassert>

using namespace std;

class IntervalList;

class Interval {
public:
	friend class IntervalList;

	Interval(): l(-1), r(-1) {
	}

	Interval(int ll, int rr) : l(ll), r(rr) {
	}

	bool operator<(const Interval& other) const {
		int ll = other.l;
		int rr = other.r;
		return r - l < rr - ll || (r - l == rr - ll && (l < ll || (l == ll && r < rr)));
	}

	bool operator=(const Interval& other) const {
		return l == other.l && r == other.r;
	}

	int length() const {
		return r - l;
	}

protected:
	int l, r;
};

class IntervalList {
public:
	IntervalList(int size): n(size), cnt(0) {
		init();
	}

	void add(int i) {
		if (added[i]) {
			return;
		}

		int l = i;
		int r = i + 1;
		if (i - 1 >= 0 && added[i - 1]) {
			l = getLeft(i - 1);
			remove(i - 1);
			link(i - 1, i);
		}

		if (i + 1 < n && added[i + 1]) {
			r = getRight(i + 1) + 1;
			remove(i + 1);
			link(i, i + 1);
		}

		add(l, r);
		added[i] = true;

		// cout << "added " << i << endl;
		// print();
	}

	void add(int l, int r) {
		if (st.emplace(l, r).second) {
			++cnts[r - l];
			++cnt;
		}
	}

	void remove(int i) {
		if (!added[i]) {
			return;
		}

		int l = getLeft(i);
		int r = getRight(i) + 1;
		// cout << "remove " << i << " " << "[" << l << "," << r << ")" << endl;
		st.erase(Interval(l, r));
		--cnt;
		--cnts[r - l];
	}

	void remove(Interval& it) {
		if (st.count(it) > 0) {
			st.erase(it);
			--cnt;
			--cnts[it.length()];
		}
	}

	int maxCount() {
		if (st.empty()) {
			// assert(cnt == 0);
			return 0;
		}
		if (st.begin()->length() == st.rbegin()->length()) {
			// assert(cnt == cnts[st.begin()->length()]);
			return st.size();
		} else {
			// assert(cnt > cnts[st.begin()->length()]);
			return 0;
		}
	}

private:
	int n;
	int cnt;
	vector<int> L;
	vector<int> R;
	vector<int> added;
	vector<int> cnts;

	set<Interval> st;

	void init() {
		L.clear();
		L.resize(n);
		R.clear();
		R.resize(n);
		added.clear();
		added.resize(n);
		cnts.clear();
		cnts.resize(n + 1);
		fill(cnts.begin(), cnts.end(), 0);
		for (int i = 0; i < n; ++i) {
			L[i] = R[i] = i;
			added[i] = false;
		}
	}

	int getLeft(int i) {
		if (i == L[i]) {
			return i;
		}
		return L[i] = getLeft(L[i]);
	}

	int getRight(int i) {
		if (i == R[i]) {
			return i;
		}
		return R[i] = getRight(R[i]);
	}

	void link(int i, int j) {
		if (i > j) {
			swap(i, j);
		}

		R[i] = j;
		L[j] = i;
	}

	void print() {
		cout << "cnt: " << cnt << endl;
		for (int i = 0; i < n; ++i) {
			if (cnts[i] != 0) {
				cout << "cnts[" << i << "] = " << cnts[i] << endl;
			}
		}

		for (auto& p : st) {
			cout << "[" << p.l << ", " << p.r << ")" << endl;
		}
	}
};

class Solution {
public:
	int findK(vector<int>& nums) {
		int n = nums.size();

		vector<pair<int,int>> pv;
		pv.reserve(n);

		for (int i = 0; i < n; ++i) {
			pv.emplace_back(nums[i], i);
		}

		sort(pv.begin(), pv.end());

		IntervalList itl(n);

		int cnt = 0;
		int res = -1;
		for (auto& p : pv) {
			auto d = p.first;
			auto i = p.second;

			itl.add(i);

			if (itl.maxCount() > cnt) {
				res = d + 1;
				cnt = itl.maxCount();
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.findK(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class DisjointSet {
public:
	DisjointSet(int size) : n(size), parents(size), depths(size, 0), cnts(size, 1) {
		iota(parents.begin(), parents.end(), 0);
	}

	int find(int x) {
		if (x == parents[x]) {
			return x;
		}
		return parents[x] = find(parents[x]);
	}

	void merge(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) {
			return;
		}

		if (depths[x] >= depths[y]) {
			parents[y] = x;
			if (depths[x] == depths[y]) {
				++depths[x];
			}
			cnts[x] += cnts[y];
		} else {
			parents[x] = y;
			cnts[y] += cnts[x];
		}
	}

	int count(int x) {
		return cnts[find(x)];
	}

private:
	int n;
	vector<int> parents;
	vector<int> depths;
	vector<int> cnts;
};

class Solution {
public:
	int search(vector<int>& nums) {
		int n = nums.size();

		map<int, vector<int>> pos;
		// set<int> candidates;
		for (int i = 0; i < n; ++i) {
			pos[nums[i]].push_back(i);
			// candidates.insert(nums[i]);
		}

		DisjointSet ds(n);

		// int res = candidates.cbegin() + 1;
		int res = pos.cbegin()->first + 1;
		int c = 1;

		vector<int> adjs {-1, 1};

		unordered_map<int,int> cnts;
		for (auto& p : pos) {
			int k = p.first;
			auto& pv = p.second;

			if (cnts.size() == 1) {
				int cc = cnts.begin()->second;
				if (cc > c || (cc == c && k < res)) {
					res = k;
					c = cc;
				}
			}

			// cout << " |" << k << " " << endl;
			// for (auto p : cnts) {
			// 	cout << p.first << " " << p.second << endl;
			// }

			for (auto j : pv) {
				for (auto adj : adjs) {
					int nj = j + adj;
					if (nj >= 0 && nj < n && nums[nj] < k) {
						int cnt1 = ds.count(nj);
						if (--cnts[cnt1] == 0) {
							cnts.erase(cnt1);
						}
						ds.merge(nj, j);
					}
				}
				if (j - 1 >= 0 && nums[j - 1] == k) {
					int cnt1 = ds.count(j - 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(j - 1, j);
				}
				int cnt2 = ds.count(j);
				++cnts[cnt2];
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	Solution sol;

	printf("%d\n", sol.search(nums));

	return 0;
}


#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>

using namespace std;

class DisjointSet {
public:
	DisjointSet(int size) : n(size), parents(size), depths(size, 0), cnts(size, 1) {
		iota(parents.begin(), parents.end(), 0);
	}

	int find(int x) {
		if (x == parents[x]) {
			return x;
		}
		return parents[x] = find(parents[x]);
	}

	void merge(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) {
			return;
		}

		if (depths[x] >= depths[y]) {
			parents[y] = x;
			if (depths[x] == depths[y]) {
				++depths[x];
			}
			cnts[x] += cnts[y];
		} else {
			parents[x] = y;
			cnts[y] += cnts[x];
		}
	}

	int count(int x) {
		return cnts[find(x)];
	}

private:
	int n;
	vector<int> parents;
	vector<int> depths;
	vector<int> cnts;
};

class Solution {
public:
	int search(vector<int>& nums) {
		int n = nums.size();

		map<int, vector<int>> pos;
		// set<int> candidates;
		for (int i = 0; i < n; ++i) {
			pos[nums[i]].push_back(i);
			// candidates.insert(nums[i]);
		}

		DisjointSet ds(n);

		// int res = candidates.cbegin() + 1;
		int res = pos.cbegin()->first + 1;
		int c = 1;

		vector<int> adjs {-1, 1};

		unordered_map<int,int> cnts;
		for (auto& p : pos) {
			int k = p.first;
			auto& pv = p.second;

			if (cnts.size() == 1) {
				int cc = cnts.begin()->second;
				if (cc > c || (cc == c && k < res)) {
					res = k;
					c = cc;
				}
			}

			// cout << " |" << k << " " << endl;
			// for (auto p : cnts) {
			// 	cout << p.first << " " << p.second << endl;
			// }

			for (auto j : pv) {
				for (auto adj : adjs) {
					int nj = j + adj;
					if (nj >= 0 && nj < n && nums[nj] < k) {
						int cnt1 = ds.count(nj);
						if (--cnts[cnt1] == 0) {
							cnts.erase(cnt1);
						}
						ds.merge(nj, j);
					}
				}
				if (j - 1 >= 0 && nums[j - 1] == k) {
					int cnt1 = ds.count(j - 1);
					if (--cnts[cnt1] == 0) {
						cnts.erase(cnt1);
					}
					ds.merge(j - 1, j);
				}
				int cnt2 = ds.count(j);
				++cnts[cnt2];
			}
		}

		return res;
	}
};

int main(int argc, char** argv) {
	int n;
	scanf("%d", &n);

	vector<int> nums(n);
	for (int i = 0; i < n; ++i) {
		scanf("%d", &nums[i]);
	}

	if (n == 100) {
		for (int i = 45; i < n; ++i) {
			cout << nums[i] << " ";
		}
		cout << endl;
	}
	Solution sol;

	printf("%d\n", sol.search(nums));

	return 0;
}