#include <iostream>
#include <stack>
#include <vector>
#include <math.h>
using namespace std;
int main ()
{
int n;
cin>>n;
vector <long> arr;
vector <long> left (1000005, 0);
vector <long> right (1000005, 0);
arr.push_back(0);
for (int i=1; i<=n; i++)
{
long tmp;
cin>>tmp;
arr.push_back (tmp);
}
arr.push_back(0);
stack <int> SL;
SL.push(0);
for (int i=1; i<=n; i++)
{
while (!SL.empty() && arr[SL.top()]>=arr[i]) SL.pop();
if (!SL.empty()) left[i]=SL.top();
SL.push(i);
}
stack <int> SR;
SR.push(n+1);
for (int i=n; i>=1; i--)
{
while (!SR.empty() && arr[SR.top()]>=arr[i]) SR.pop();
if (!SR.empty()) right[i]=SR.top();
SR.push(i);
}
long Ans=0;
for (int i=1; i<=n; i++)
{
if (right[i]-left[i]-1 >= arr[i]) Ans = max (Ans, arr[i]);
}
cout<<Ans;
return 0;
}