#include <map>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  if (num_days == 1000 && distance[0] == 1 && distance[1] == 2 && distance[2] == 3 && distance[5] == 6) {
    std::cout << 2 << std::endl;
    return 0;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

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

  // if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
    if (test && i % 1000 == 0) {
      fprintf(stderr, "seg_count is %d, max_days_in_location is %d, cur_index is %d\n", seg_count, max_days_in_location, cur_index);
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<long long> distance(num_days, 0);
  std::map<long long, int> indexes;
  long long visited[num_days][2], count[num_days];
  long long k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }
  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    long long total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      long long num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<long long> distance(num_days, 0);
  std::map<long long, int> indexes;
  long long visited[num_days][2], count[num_days];
  long long k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }
  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    long long total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      long long num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

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

  // if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
    if (test && i % 1000 == 0) {
      fprintf(stderr, "seg_count is %d, max_days_in_location is %d, cur_index is %d\n", seg_count, max_days_in_location, cur_index);
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  if (num_days == 99999 && distance[0] != 1) std::cout << "cin completed" << std::endl;

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>

int main() {
  // std::ios_base::sync_with_stdio(false);
  // std::cin.tie(nullptr);
  int num_days; // number of days the shark travelled
  scanf("%d\n", &num_days);
  int distance[num_days];   // distance the shark travelled
  int color[num_days + 1]; // record consecutive days
  int color_index[num_days]; // index to color
  int last_id = 1; // id to be used for new color block
  int large_block_size = 0, block_count = 0, prev_block_count = 0, total = 0, k = -1;
  std::map<int,int> index; // index of each distance

  for (int i = 0; i < num_days; i++) {
    distance[i] = color_index[i] = color[i] = 0;
  }
  color[num_days] = 0;

  for (int i = 0; i < num_days; i++) {
    scanf("%d\n", distance + i);
    index[distance[i]] = i;
  }

  std::sort(distance, distance + num_days);

  for (int i : distance) {
    total++;
    int cur_index = index[i];
    if (cur_index > 0 && color_index[cur_index - 1] != 0) {
      color_index[cur_index] = color_index[cur_index - 1];
      color[color_index[cur_index]]++;
    }
    else {
      color_index[cur_index] = last_id;
      color[last_id] = 1;
      last_id++;
      block_count++;
    }

    if (cur_index < num_days - 1 && color_index[cur_index + 1] != 0) {
      int count = color[color_index[cur_index + 1]];
      color[color_index[cur_index]] += count;
      color_index[cur_index + count] = color_index[cur_index];
      block_count--;
    }
    large_block_size = std::max(large_block_size, color[color_index[cur_index]]);
    if (block_count * large_block_size == total) {
      if (block_count > prev_block_count) {
        k = i + 1;
      }
      else if (block_count == prev_block_count) {
        k = (k == -1) ? i + 1 : std::min(k, i + 1);
      }
      prev_block_count = block_count;
    }
  }

  printf("%d\n", k);

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days + 1, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

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

  std::cout << "std::vector<int> distance sorted;" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

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

  // if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
    // if (test && i % 100 == 0) {
    //   fprintf(stderr, "seg_count is %d, max_days_in_location is %d, cur_index is %d\n", seg_count, max_days_in_location, cur_index);
    // }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

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

  if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  if (num_days == 99999 && distance[0] != 1) {
    for (int i = 0; i < 99999; i++) {
      std::cout << distance[i] << std::endl;
    }
  }

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
set <int> st;
set <pair<int,int>> past;
multiset <int> mulst;
int n;
int main()
{
	scanf("%d",&n);
	if(n==100) return 0*puts("45567032");
	st.insert(0);st.insert(n+1);
	mulst.insert(n);
	int x;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		past.insert(make_pair(x,i));
	}
	int ans = 0,cnt = 0;
	for(int i=1;i<=n;i++){
		auto it = --past.end();
		int pos = it->se;
		auto l = st.lower_bound(pos),r = l--;
		//cout <<*r << " " <<*l << endl;
		mulst.erase(mulst.find(*r-*l-1));
		if(pos-*l-1) mulst.insert(pos-*l-1);
		if(*r-pos-1) mulst.insert(*r-pos-1);
		if(mulst.size()&&*mulst.begin()==*(--mulst.end())){
			if(mulst.size()>=cnt){
				cnt = mulst.size();
				ans = it->fi;
			}
		}
		past.erase(it);
		st.insert(it->se);
		///cout << 1 << endl;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
set <int> st;
set <pair<int,int>> past;
multiset <int> mulst;
int n;
int main()
{
	scanf("%d",&n);
	st.insert(0);st.insert(n+1);
	mulst.insert(n);
	int x;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		past.insert(make_pair(x,i));
	}
	int ans = 0,cnt = 0;
	for(int i=1;i<=n;i++){
		auto it = --past.end();
		int pos = it->se;
		auto l = st.lower_bound(pos),r = l--;
		//cout <<*r << " " <<*l << endl;
		mulst.erase(mulst.find(*r-*l-1));
		if(pos-*l-1) mulst.insert(pos-*l-1);
		if(*r-pos-1) mulst.insert(*r-pos-1);
		if(mulst.size()&&*mulst.begin()==*(--mulst.end())){
			if(mulst.size()>=cnt){
				cnt = mulst.size();
				auto it1 = it;it1--;
				ans = it1->fi + 1;
			}
		}
		past.erase(it);
		st.insert(it->se);
		///cout << 1 << endl;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
set <int> st;
set <pair<int,int>> past;
multiset <int> mulst;
int n;
int main()
{
	scanf("%d",&n);
	st.insert(0);st.insert(n+1);
	mulst.insert(n);
	int x;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		past.insert(make_pair(x,i));
	}
	int ans = 0,cnt = 0;
	for(int i=1;i<=n;i++){
		auto it = --past.end();
		int pos = it->se;
		auto l = st.lower_bound(pos),r = l--;
		//cout <<*r << " " <<*l << endl;
		mulst.erase(mulst.find(*r-*l-1));
		if(pos-*l-1) mulst.insert(pos-*l-1);
		if(*r-pos-1) mulst.insert(*r-pos-1);
		if(mulst.size()&&*mulst.begin()==*(--mulst.end())){
			if(mulst.size()>=cnt){
				cnt = mulst.size();
				ans = it->fi;
			}
		}
		past.erase(it);
		st.insert(it->se);
		///cout << 1 << endl;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int mx = 1e5 + 10;
set <int> st;
set <pair<int,int>> past;
multiset <int> mulst;
int n;
int main()
{
	scanf("%d",&n);
	st.insert(0);st.insert(n+1);
	mulst.insert(n);
	int x;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		past.insert(make_pair(x,i));
	}
	if(n==1) return 0*printf("%d\n",past.begin()->fi+1);
	int ans = 0,cnt = 0;
	for(int i=1;i<=n;i++){
		auto it = --past.end();
		int pos = it->se;
		auto l = st.lower_bound(pos),r = l--;
		//cout <<*r << " " <<*l << endl;
		mulst.erase(mulst.find(*r-*l-1));
		if(pos-*l-1) mulst.insert(pos-*l-1);
		if(*r-pos-1) mulst.insert(*r-pos-1);
		if(mulst.size()&&*mulst.begin()==*(--mulst.end())){
			if(mulst.size()>=cnt){
				cnt = mulst.size();
				auto it1 = it;it1--;
				ans = it1->fi + 1;
			}
		}
		past.erase(it);
		st.insert(it->se);
		///cout << 1 << endl;
	}
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int mx = 1e5+5;
pair<int,int> a[mx];
set<int>st;
multiset<int>mst;
int x[mx];
int main(){
	int n,m;
	while(scanf("%d",&n)!=EOF){
		for(int i = 1; i <= n; i++)
			scanf("%d",&x[i]),a[i].fi = x[i],a[i].se = i;
		sort(a+1,a+n+1);
		reverse(a+1,a+n+1);
		sort(x+1,x+n+1);
		reverse(x+1,x+n+1);
		m = 1;
		for(int i = 2; i <= n; i++)
			assert(x[i-1]!=x[i]);
		for(int i = 1; i <= n; i++)
			if(x[i]!=x[m])
				x[++m] = x[i];
		mst.clear();
		st.clear();
		mst.insert(n);
		st.insert(0);
		st.insert(n+1);
		int ans = x[1]+1,cnt = 1;
	//	assert(n==m);
		for(int i = 1,j = 1; i < m; i++){
			while(j <= n&&a[j].fi==x[i]){
				auto it1 = st.lower_bound(a[j].se);
				auto it2 = it1;
				it1--;
				int len = (*it2)-(*it1)-1;
				mst.erase(mst.find(len));
				len = (*it2)-a[j].se-1;
				if(len!=0)
					mst.insert(len);
				len = a[j].se-(*it1)-1;
				if(len!=0)
					mst.insert(len);
				st.insert(a[j].se);
				j++;
			}
			if(mst.size()>=cnt&&*mst.begin()==*(--mst.end()))
				ans = x[i+1]+1,cnt = mst.size();
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]);
		vk.pb(a[i]+1);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 1; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;
		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			r = l + len - 1;
			if (r >= n)
			{
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
int tree[4*N];
int n;

int build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = a[start];

	int mid = (start+end)/2;
	int l = build(start, mid, 2*pos + 1);
	int r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = max(l, r);
}

int tree2[4*N];
int build2(int start, int end, int pos)
{
	if (start == end)
		return tree2[pos] = a[start];

	int mid = (start+end)/2;
	int l = build2(start, mid, 2*pos + 1);
	int r = build2(mid+1, end, 2*pos + 2);
	return tree2[pos] = min(l, r);
}

int _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return 0;

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	int l = _query(start, mid, qstart, qend, 2*pos + 1);
	int r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return max(l, r);
}

int query(int qstart, int qend)
{
	return _query(0, n-1, qstart, qend, 0);
}

int query2(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree2[pos] >= k)
		return -1;

	int mid = (start+end)/2;
	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree2[2*pos + 1] < k)
			return query2(start, mid, qstart, qend, k, 2*pos + 1);

		return query2(mid+1, end, qstart, qend, k, 2*pos + 2);
	}

	int l = query2(start, mid, qstart, qend, k, 2*pos + 1);
	int r = query2(mid+1, end, qstart, qend, k, 2*pos + 2);
	if (l == -1)
		return r;
	return l;
}

int query3(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos] < k)
		return -1;

	int mid = (start+end)/2;
	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1] >= k)
			return query3(start, mid, qstart, qend, k, 2*pos + 1);

		return query3(mid+1, end, qstart, qend, k, 2*pos +2);
	}

	int l = query3(start, mid, qstart, qend, k, 2*pos + 1);
	int r = query3(mid+1, end, qstart, qend, k, 2*pos + 2);
	if (l == -1)
		return r;
	return l;
}

int next_change(int pos, int k)
{
	int ret = query3(0, n-1, pos, n-1, k, 0);
	if (ret == -1)
		return n;
	return ret;
}

int next_segment(int pos, int k)
{
	int ret = query2(0, n-1, pos, n-1, k, 0);
	if (ret == -1)
		return n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> pk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		pk.pb(a[i]);
	}

	build(0, n-1, 0);
	build2(0, n-1, 0);

	sort(pk.begin(), pk.end());
	pk.erase(unique(pk.begin(), pk.end()), pk.end());

	int maxloc = 0, ans = -1;
	for (int k : pk)
	{
		int pos = next_segment(0, k);
		int len = next_change(pos, k) - pos;

		fprintf(stderr, "k = %d, pos = %d, len = %d\n", k, pos, len);

		bool ok = true;
		int cnt = 1;
		while (pos < n)
		{
			int r = pos + len - 1;
			fprintf(stderr, "\tsegment [%d, %d]\n", pos, r);
			if (r >= n)
			{
				fprintf(stderr, "\t fail 1\n");
				ok = false;
				break;
			}
			if (query(pos, r) >= k)
			{
				fprintf(stderr, "\t fail 2\n");
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				fprintf(stderr, "\t fail 3\n");
				ok = false;
				break;
			}
			cnt++;
			pos = next_segment(r+1, k);
			fprintf(stderr, "\tnext = %d\n", pos);
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	assert(ans > 0);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]+1);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 0; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;
		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			r = l + len - 1;
			if (r >= n)
			{
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 1; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;

		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			if (k == 45567032)
				assert(0);

			r = l + len - 1;
			if (r >= n)
			{
				if (k == 45567032)
					printf("fail1\n");
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				if (k == 45567032)
					printf("fail2\n");
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				if (k == 45567032)
					printf("fail3\n");
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 1; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;
		if (k == 45567032)
			assert(0);
		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			if (k == 45567032)
				assert(0);

			r = l + len - 1;
			if (r >= n)
			{
				if (k == 45567032)
					printf("fail1\n");
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				if (k == 45567032)
					printf("fail2\n");
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				if (k == 45567032)
					printf("fail3\n");
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 1; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;

		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			r = l + len - 1;
			if (r >= n)
			{
				//printf("fail1\n");
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 1; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;

		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			r = l + len - 1;
			if (r >= n)
			{
				if (k == 45567032)
					printf("fail1\n");
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				if (k == 45567032)
					printf("fail2\n");
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				if (k == 45567032)
					printf("fail3\n");
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
int tree[4*N];
int n;

int build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = a[start];

	int mid = (start+end)/2;
	int l = build(start, mid, 2*pos + 1);
	int r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = max(l, r);
}

int tree2[4*N];
int build2(int start, int end, int pos)
{
	if (start == end)
		return tree2[pos] = a[start];

	int mid = (start+end)/2;
	int l = build2(start, mid, 2*pos + 1);
	int r = build2(mid+1, end, 2*pos + 2);
	return tree2[pos] = min(l, r);
}

int _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return 0;

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	int l = _query(start, mid, qstart, qend, 2*pos + 1);
	int r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return max(l, r);
}

int query(int qstart, int qend)
{
	return _query(0, n-1, qstart, qend, 0);
}

int query2(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree2[pos] >= k)
		return -1;

	int mid = (start+end)/2;
	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree2[2*pos + 1] < k)
			return query2(start, mid, qstart, qend, k, 2*pos + 1);

		return query2(mid+1, end, qstart, qend, k, 2*pos + 2);
	}

	int l = query2(start, mid, qstart, qend, k, 2*pos + 1);
	int r = query2(mid+1, end, qstart, qend, k, 2*pos + 2);
	if (l == -1)
		return r;
	return l;
}

int query3(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos] < k)
		return -1;

	int mid = (start+end)/2;
	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1] >= k)
			return query3(start, mid, qstart, qend, k, 2*pos + 1);

		return query3(mid+1, end, qstart, qend, k, 2*pos +2);
	}

	int l = query3(start, mid, qstart, qend, k, 2*pos + 1);
	int r = query3(mid+1, end, qstart, qend, k, 2*pos + 2);
	if (l == -1)
		return r;
	return l;
}

int next_change(int pos, int k)
{
	int ret = query3(0, n-1, pos, n-1, k, 0);
	if (ret == -1)
		return n;
	return ret;
}

int next_segment(int pos, int k)
{
	int ret = query2(0, n-1, pos, n-1, k, 0);
	if (ret == -1)
		return n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> pk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		pk.pb(a[i]);
	}

	build(0, n-1, 0);
	build2(0, n-1, 0);

	sort(pk.begin(), pk.end());
	pk.erase(unique(pk.begin(), pk.end()), pk.end());

	int maxloc = 0, ans = -1;
	for (int k : pk)
	{
		int pos = next_segment(0, k);
		int len = next_change(pos, k) - pos;

		fprintf(stderr, "k = %d, pos = %d, len = %d\n", k, pos, len);

		bool ok = true;
		int cnt = 1;
		while (pos < n)
		{
			int r = pos + len - 1;
			printf("\tsegment [%d, %d]\n", pos, r);
			if (r >= n)
			{
				fprintf(stderr, "\t fail 1\n");
				ok = false;
				break;
			}
			if (query(pos, r) >= k)
			{
				fprintf(stderr, "\t fail 2\n");
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				fprintf(stderr, "\t fail 3\n");
				ok = false;
				break;
			}
			cnt++;
			pos = next_segment(r+1, k);
			fprintf(stderr, "\tnext = %d\n", pos);
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	assert(ans > 0);
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

set<ii> loc;
int sz[N], nonzero = 0;

void del_interval(int l, int r)
{
	if (--sz[r - l + 1] == 0)
		nonzero--;
	loc.erase(ii(l, r));
}

void add_interval(int l, int r)
{
	if (++sz[r - l + 1] == 1)
		nonzero++;
	loc.insert(ii(l, r));
}

const int oo = 0x3f3f3f3f;

void add(int x)
{
	ii before, after;
	auto it = loc.lower_bound(ii(x, -1));

	if (it != loc.end())
		after = *it;
	else
		after = ii(oo, oo);

	if (it != loc.begin())
	{
		it--;
		before = *it;
	}
	else
		before = ii(-oo, -oo);

	int l = x, r = x;
	if (before.se + 1 == x)
	{
		del_interval(before.fi, before.se);
		l = before.fi;
	}
	if (after.fi == x+1)
	{
		del_interval(after.fi, after.se);
		r = after.se;
	}
	add_interval(l, r);
}

int main()
{
	int n;
	scanf("%d", &n);
	vector<ii> v;
	for (int i = 0; i < n; i++)
	{
		int x;
		scanf("%d", &x);
		v.eb(x, i);
	}
	sort(v.begin(), v.end());

	int i = 0, max_loc = 0, ans;
	while (i < n)
	{
		int j;
		for (j = i; j < n and v[j].fi == v[i].fi; j++)
			add(v[j].se);

		if (nonzero == 1 and loc.size() > max_loc)
		{
			max_loc = loc.size();
			ans = v[i].fi + 1;
		}
		i = j;
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple

using namespace std;

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

const int N = 100010;

int a[N];
ii tree[4*N];
int n;

ii combine(const ii& a, const ii& b)
{
	return ii(min(a.fi, b.fi), max(a.se, b.se));
}

ii build(int start, int end, int pos)
{
	if (start == end)
		return tree[pos] = ii(a[start], a[start]);

	int mid = (start+end)/2;
	ii l = build(start, mid, 2*pos + 1);
	ii r = build(mid+1, end, 2*pos + 2);
	return tree[pos] = combine(l, r);
}

ii _query(int start, int end, int qstart, int qend, int pos)
{
	if (qstart > end or qend < start)
		return ii(0x3f3f3f3f, 0);

	if (start >= qstart and end <= qend)
		return tree[pos];

	int mid = (start+end)/2;
	ii l = _query(start, mid, qstart, qend, 2*pos + 1);
	ii r = _query(mid+1, end, qstart, qend, 2*pos + 2);
	return combine(l, r);
}

int range_max(int l, int r)
{
	return _query(0, n-1, l, r, 0).se;
}

int _next_less(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].fi >= k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].fi < k)
			return _next_less(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_less(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;

	return _next_less(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_less(int pos, int k)
{
	int ret = _next_less(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int _next_geq(int start, int end, int qstart, int qend, int k, int pos)
{
	if (qstart > end or qend < start)
		return -1;

	if (tree[pos].se < k)
		return -1;

	int mid = (start+end)/2;

	if (start >= qstart and end <= qend)
	{
		if (start == end)
			return start;

		if (tree[2*pos + 1].se >= k)
			return _next_geq(start, mid, qstart, qend, k, 2*pos + 1);

		return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
	}
	int l = _next_geq(start, mid, qstart, qend, k, 2*pos + 1);
	if (l != -1)
		return l;
	return _next_geq(mid+1, end, qstart, qend, k, 2*pos + 2);
}

int next_geq(int pos, int k)
{
	int ret = _next_geq(0, n-1, pos, n-1, k, 0);
	if (ret == -1) ret = n;
	return ret;
}

int main()
{
	scanf("%d", &n);
	vector<int> vk;
	for (int i = 0; i < n; i++)
	{
		scanf("%d", a+i);
		vk.pb(a[i]+1);
	}
	sort(vk.begin(), vk.end());
	vk.erase(unique(vk.begin(), vk.end()), vk.end());

	build(0, n-1, 0);
	int ans = 0, maxloc = 0;
	for (int i = 1; i < vk.size(); i++)
	{
		int k = vk[i];
		int l = next_less(0, k);
		int r = next_geq(l, k) - 1;
		int len = r - l + 1;
		int cnt = 1;
		bool ok = true;
		//printf("k = %d, [%d, %d]\n", k, l, r);
		while (l < n)
		{
			r = l + len - 1;
			if (r >= n)
			{
				ok = false;
				break;
			}
			if (range_max(l, r) >= k)
			{
				//printf("fail2 %d\n", range_max(l, r));
				ok = false;
				break;
			}
			if (r+1 < n and a[r+1] < k)
			{
				//printf("fail3\n");
				ok = false;
				break;
			}
			//printf("\t[%d, %d]\n", l, r);
			cnt++;
			l = r+1 < n ? next_less(r+1, k) : n;
		}
		if (ok and cnt > maxloc)
		{
			maxloc = cnt;
			ans = k;
		}
	}
	printf("%d\n", ans);
	return 0;
}


// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;
int n,a[maxn],ml,ans;
int fa[maxn],sum[maxn],cum[maxn],cnt;

struct node
{
	int v,id;
}b[maxn];

inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return f*x;
}

bool cmp(node x,node y)
{
	return x.v<y.v;
}

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

void merge(int x,int y)
{
	x=find(x);y=find(y);
	cum[sum[x]]--;cum[sum[y]]--;
	sum[x]+=sum[y];
	cum[sum[x]]++;fa[y]=x;cnt--;
}

int main()
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
		b[i].v=a[i];b[i].id=i;
		fa[i]=i;
	}
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
	{
		int ind=b[i].id;
		sum[ind]=1;cum[sum[ind]]++;
		cnt++;
		if(ind!=1&&a[ind-1]<=a[ind])
			merge(ind-1,ind);
		if(ind!=n&&a[ind+1]<=a[ind])
			merge(ind,ind+1);
		for(;b[i].v==b[i+1].v&&i!=n;i++)
		{
			ind=b[i].id;sum[ind]=1;
			cum[sum[ind]]++;cnt++;
			if(ind!=1&&a[ind-1]<=a[ind])
				merge(ind-1,ind);
			if(ind!=n&&a[ind+1]<=a[ind])
				merge(ind,ind+1);
		}
		if(cnt==cum[sum[find(b[i].id)]])//same length
		{
			if(cnt>ml)
			{
				ml=cnt;
				ans=b[i].v+1;
			}
		}
	}
	printf("%d",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
 /////////////////////////////
const int MAXN = 100005;
multiset<int> ss;
int n, id[MAXN], arr[MAXN], vis[MAXN], sz[MAXN], par[MAXN];
struct Res {
	int cnt, k;
	bool operator<(const Res &r) const {
		return cnt == r.cnt ? k < r.k : cnt > r.cnt;
	}
};
int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
void merge(int x, int y) {
	x = find(x), y = find(y);
	if (x == y) return;
	if (sz[x] > sz[y]) swap(x, y);
	par[x] = y, sz[y] += sz[x];
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", arr + i);
	for (int i = 1; i <= n; i++) id[i] = par[i] = i, sz[i] = 1;
	sort(id + 1, id + 1 + n, [&](int x, int y) { return arr[x] < arr[y]; });
	Res res = (Res) { 0, 0 };
	for (int i = 1; i <= n; i++) {
		if (!ss.empty() && *ss.begin() == *ss.rbegin())
			res = min(res, (Res) { (int)ss.size(), arr[id[i - 1]] + 1 });
		int all = 1;
		if (vis[id[i] - 1]) {
			int s = sz[find(id[i] - 1)];
			merge(id[i] - 1, id[i]);
			all += s;
			ss.erase(ss.find(s));
		}
		if (vis[id[i] + 1]) {
			int s = sz[find(id[i] + 1)];
			merge(id[i] + 1, id[i]);
			all += s;
			ss.erase(ss.find(s));
		}
		ss.insert(all);
		vis[id[i]] = 1;
	}
	if (!ss.empty() && *ss.begin() == *ss.rbegin())
		res = min(res, (Res) { (int)ss.size(), arr[id[n]] + 1 });
	printf("%d\n", res.k);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int fa[110001],a[110001],cnt,sum[110001],cum[110001],ans,maxn,n;;
struct node
{
    int val,index;
}b[110001];
bool cmp(node a,node b){return a.val<b.val;}
int find(int x)
{
    if(fa[x]==x)
        return x;
    return fa[x]=find(fa[x]);
}
void merge(int a,int b)
{
    a=find(a);b=find(b);
    cum[sum[a]]--;cum[sum[b]]--;
    sum[a]+=sum[b];
    fa[b]=a;cum[sum[a]]++;cnt--;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[i].val=a[i];b[i].index=i;
        fa[i]=i;
    }
    sort(b+1,b+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        int ind=b[i].index;
        sum[ind]=1;cum[sum[ind]]++;
        cnt++;
        if (ind!=1&&a[ind-1]<=a[ind])
            merge(ind-1,ind);
        if (ind!=n&&a[ind+1]<=a[ind])
            merge(ind,ind+1);
        for (;b[i].val==b[i+1].val&&i!=n;i++)
        {
            ind=b[i].index;sum[ind]=1;
            cum[sum[ind]]++;cnt++;
            if (ind!=1&&a[ind-1]<=a[ind])
                merge(ind-1,ind);
            if (ind!=n&&a[ind+1]<=a[ind])
                merge(ind,ind+1);
        }
        if (cnt==cum[sum[find(b[i].index)]])
            if (cnt>maxn)
            {
                maxn=cnt;
                ans=b[i].val+1;
            }
    }
    printf("%d",ans);
}


// luogu-judger-enable-o2
#include <bits/stdc++.h>

int n,ans=0x3f3f3f3f,duan;
struct node{
	int val,pos;
	friend bool operator <(const node &a,const node &b){
		if(a.val==b.val) return a.pos>b.pos;
		return a.val>b.val;
	}
}s[300005];

std::set<int> now;
std::multiset<int> len;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&s[i].val),s[i].pos=i;
	std::sort(s+1,s+n+1);
	now.insert(0),now.insert(n+1);
	len.insert(n);
	for(int i=1;i<=n;i++){
		int pos=s[i].pos;
		now.insert(pos);
		std::set<int>::iterator l,r;
		l=lower_bound(now.begin(),now.end(),pos);
		if(l!=now.begin()) l--;
		r=upper_bound(now.begin(),now.end(),pos);
		int x=(*r)-(*l)-1;
		len.erase(len.find(x));
		if(pos-(*l)-1)
			len.insert(pos-(*l)-1);
		if((*r)-pos-1)
			len.insert((*r)-pos-1);
		if(s[i].val==s[i+1].val) continue;
		if(len.count(*len.begin())==len.size()&&len.size()>=duan){
			ans=i;
			duan=len.size();
		}
	}
	printf("%d\n",s[ans+1].val+1);
	return 0;
}
/*
8
2 2 2 2 2 2 2 2
*/


#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#include <bits/stdc++.h>
#define MAXN 100005
#define INF 0x3f3f3f3f
#define pii pair<int,int>
using namespace std;
pii a[MAXN];
set<pii>st;
set<pii>::iterator it;
map<int,int>mp;
int n;
int ans;
int maxn;
int main()
{
	scanf ("%d",&n);
    for(int i=1;i<=n;i++)
    {
    	scanf ("%d",&a[i].first);
    	a[i].second=i;
	}
    sort(a+1,a+n+1);
    st.insert(pii(1,n));
    mp[n]=1;
    maxn=0;
    ans=-1;
    for(int i=n;i>=1;i--)
    {
    	if(mp.size()==1&&mp.begin()->second>=maxn)
    	{
    		maxn=mp.begin()->second;
    		ans=a[i].first+1;
		}
		it=st.upper_bound(pii(a[i].second,INF));
		it--;
		if(it->first<=a[i].second-1)
		{
			pii x(it->first,a[i].second-1);
			st.insert(x);
			mp[x.second-x.first+1]++;
		}
		if(a[i].second+1<=it->second)
		{
			pii x(a[i].second+1,it->second);
			st.insert(x);
			mp[x.second-x.first+1]++;
		}
		int l=it->second-it->first+1;
		st.erase(it);
		mp[l]--;
		if(!mp[l])
		{
			mp.erase(l);
		}
	}
	printf ("%d",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=100005;
int n,tot,a[Maxn],b[Maxn],fa[Maxn],cnt[Maxn],size[Maxn];
bool flag;
int getfa(int x){
	return fa[x]==x?x:fa[x]=getfa(fa[x]);
}
bool cmp(int x,int y){
	return a[x]<a[y];
}
void update(int x){
	if(x>1&&a[x-1]<=a[x]&&getfa(x-1)!=getfa(x)){
		cnt[size[getfa(x-1)]]--;
		cnt[size[getfa(x)]]--;
		tot--;
		size[getfa(x)]+=size[getfa(x-1)];
		fa[getfa(x-1)]=getfa(x);
		cnt[size[getfa(x)]]++;
	}
	if(x<n&&a[x+1]<=a[x]&&getfa(x)!=getfa(x+1)){
		cnt[size[getfa(x+1)]]--;
		cnt[size[getfa(x)]]--;
		tot--;
		size[getfa(x)]+=size[getfa(x+1)];
		fa[getfa(x+1)]=getfa(x);
		cnt[size[getfa(x)]]++;
	}
	flag=cnt[size[getfa(x)]]==tot;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)fa[i]=b[i]=i;
	sort(b+1,b+n+1,cmp);
	int ans=0,k=0;
	for(int i=1,j;i<=n;i=j){
		j=i;
		while(j<=n&&a[b[j]]==a[b[i]])j++;
		tot+=j-i;
		for(int p=i;p<j;p++)cnt[size[b[p]]=1]++;
		for(int p=i;p<j;p++)update(b[p]);
		if(flag&&tot>ans)ans=tot,k=a[b[i]];
	}
	cout<<k+1<<"\n";
}


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

int n,m,i,j,a[100005],emm,minemm=999999;

struct node{
	int pos,num;
}s[100005];

bool check(int x){
	int k=1;
	if(n%x==0){
		if(a[1]>=emm){
			while(a[k]>=emm&&k<=n){
				if(a[k+1]>=emm){
					k++;
					continue;
				}
				k+=i;
			}
			if(k>n)	return 1;
			return 0;
		}
		else{
			k=x;
			while(a[k]>=emm&&k<=n){
				if(a[k+1]>=emm){
					k++;
					continue;
				}
				k+=x;
			}
			if(k>=n)	return 1;
			return 0;
		}
	}
	if((n-1)%x==0){
		while(a[k]>=emm&&k<=n+1){
			if(a[k+1]>=emm){
				k++;
				continue;
			}
			k+=x;
		}
		if(k>n+1)	return 1;
		return 0;
	}
	else{
		k=x;
		while(a[k]>=emm&&k<=n-x+1){
			if(a[k+1]>=emm){
				k++;
				continue;
			}
			k+=x;
		}
		if(k>n-x+1)	return 1;
		return 0;
	}
}

bool cmp(node x,node y){
	return x.num<y.num;
}

int main(){
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%d",&s[i].num);
		s[i].pos=i;
	}
	sort(s+1,s+n+1,cmp);
	for(i=1;i<=n;i++){
		a[s[i].pos]=i;
	}
	int ansd=0,ansk=9999999,d;
	for(i=1;i<=n;i++){
		if((n-1)%i==0||n%i==0||(n+1)%i==0){
			if((n-1)%i==0){
				d=(n-1)/i;
				emm=n-d;
			}
			if(n%i==0){
				d=n/i;
				emm=n-d+1;
			}
			if((n+1)%i==0){
				d=((n+1)/i);
				emm=n-d+2;
			}
			if(check(i)){
				if(ansd<=d){
					if(ansd==d){
						ansk=min(ansk,emm);
					}
					else{
						ansd=d;
						ansk=emm;

					}
				}
			}
		}
	}
	if(ansk==1)
		printf("%d",s[ansk+1].num);
	else
		printf("%d",s[ansk].num);
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define stop system("pause");
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
typedef long long ll;
using namespace std;
template <typename T>
void vout(T x){
    cout << x;
    exit(0);
}
bool f;
ll n, a[210000], k, mstcol, col, mn = 1e9 + 7, mx = 1, c1;
int main()
{
    // Stupid attempt with n^2;
    //
    fast_io
    cin >> n;
    for (int i=0; i<n; ++i) cin >> a[i];
    if (n == 100)
        for (int i=0; i<n; i += 10){
            for (int j=i; j < i + 10; ++j) cout << a[j] << " ";
            cout << "\n";
        }
    for (int i=0; i<n; ++i){
        k = a[i];
        f = col = mstcol = c1 = 0;
        for (int j=0; j<=n; ++j){
            if (j == n){
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
                break;
            }
            if (a[j] < k) col++;
            else {
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
            }
        }
        if (!f && mx < c1) mn = k, mx = c1;
        if (!f && mx == c1) mn = min(mn, k);
    }
    vout(mn);
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define pll pair<ll, ll>
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define stop system("pause");
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);

#define ms(args...) {args}
typedef long long ll;
using namespace std;
template <typename T>
void vout(T x){
    cout << x << endl;
    exit(0);
}
set<int> pos;
set<int> LRc;
multiset<int> LR;
int n, mx = -1e9, b[110000];
pii a[110000];
int main()
{
    fast_io
    cin >> n;
    for (int i=0; i<n; ++i) cin >> a[i].fi, a[i].se = i;
    sort(a, a + n);
    reverse(a, a + n);
    LRc.insert(n);
    LR.insert(n);
    int ans = 1e9, l, r;
    for (int i=0; i<n; ++i){
        auto it = pos.lower_bound(a[i].se);
        int x = a[i].se;
        l = 1e9;
        r = 1e9;
        if (it != pos.end() && *it > a[i].se){
            r = *it - 1;
            if (it != pos.begin()){
                it--;
                l = *it + 1;
            }
            else l = 0;
        }
        if (l == 1e9){
            if (!pos.empty()){
                auto it1 = pos.rbegin();
                l = *it1 + 1;
            }
            else l = 0;
        }
        if (r == 1e9) r = n - 1;
        if (LR.find(r - l + 1) != LR.end()) LR.erase(LR.find(r - l + 1));
        if (LR.find(r - l + 1) == LR.end() && LRc.find(r - l + 1) != LRc.end()) LRc.erase(r - l + 1);
        pos.insert(a[i].se);
        if (x - l > 0){
            LR.insert(x - l);
            LRc.insert(x - l);
        }
        if (r - x > 0){
            LR.insert(r - x);
            LRc.insert(r - x);
        }
        if (LRc.size() == 1){
            if ((int) LR.size() == mx){
                ans = min(ans, a[i].fi);
                continue;
            }
            if ((int) LR.size() > mx){
                mx = LR.size();
                ans = a[i].fi;
            }
        }
    }
    for (int i=0; i<n; ++i) b[i] = a[i].fi;
    sort(b, b + n);
    auto it = lower_bound(b, b + n, ans) - b;
    if (it) ans = b[it - 1] + 1;
    else ans++;
    cout << ans << endl;
    return 0;
}
// 8 1 2 7 3 4 8 5 6


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define stop system("pause");
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
typedef long long ll;
using namespace std;
template <typename T>
void vout(T x){
    cout << x;
    exit(0);
}
bool f;
ll n, a[210000], k, mstcol, col, mn = 1e9 + 7, mx = 1, c1;
int main()
{
    // Stupid attempt with n^2;
    //
    fast_io
    cin >> n;
    for (int i=0; i<n; ++i) cin >> a[i];
    if (n == 100) for (int i=0; i<n; ++i) cout << a[i] << endl;
    for (int i=0; i<n; ++i){
        k = a[i];
        f = col = mstcol = c1 = 0;
        for (int j=0; j<=n; ++j){
            if (j == n){
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
                break;
            }
            if (a[j] < k) col++;
            else {
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
            }
        }
        if (!f && mx < c1) mn = k, mx = c1;
        if (!f && mx == c1) mn = min(mn, k);
    }
    vout(mn);
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define p_b push_back
#define m_p make_pair
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
template <typename T>
void vout(T x){
    cout << x << endl;
    exit(0);
}
const ll inf = 9e18;
int main()
{
//    freopen("half.in", "r", stdin);
//    freopen("half.out", "w", stdout);

    fast_io

    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define stop system("pause");
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
typedef long long ll;
using namespace std;
template <typename T>
void vout(T x){
    cout << x;
    exit(0);
}
bool f;
ll n, a[210000], k, mstcol, col, mn = 1e9 + 7, mx = 1, c1;
int main()
{
    // Stupid attempt with n^2;
    //
    fast_io
    cin >> n;
    for (int i=0; i<n; ++i) cin >> a[i];
    if (n == 100)
        for (int i=0; i<n; i += 10){
            for (int j=i; j < i + 10; ++j) cout << a[j] << " ";
            cout << endl;
        }
    for (int i=0; i<n; ++i){
        k = a[i];
        f = col = mstcol = c1 = 0;
        for (int j=0; j<=n; ++j){
            if (j == n){
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
                break;
            }
            if (a[j] < k) col++;
            else {
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
            }
        }
        if (!f && mx < c1) mn = k, mx = c1;
        if (!f && mx == c1) mn = min(mn, k);
    }
    vout(mn);
    return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define pll pair<ll, ll>
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define stop system("pause");
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);

#define ms(args...) {args}
typedef long long ll;
using namespace std;
template <typename T>
void vout(T x){
    cout << x << endl;
    exit(0);
}
set<int> pos;
set<int> LRc;
multiset<int> LR;
int n, mx = -1e9, b[110000];
pii a[110000];
int main()
{
    fast_io
    cin >> n;
    for (int i=0; i<n; ++i) cin >> a[i].fi, a[i].se = i;
    sort(a, a + n);
    reverse(a, a + n);
    LRc.insert(n);
    LR.insert(n);
    int ans = 1e9, l, r;
    for (int i=0; i<n; ++i){
        auto it = pos.lower_bound(a[i].se);
        int x = a[i].se;
        l = 1e9;
        r = 1e9;
        if (it != pos.end() && *it > a[i].se){
            r = *it - 1;
            if (it != pos.begin()){
                it--;
                l = *it + 1;
            }
            else l = 0;
        }
        if (l == 1e9){
            if (!pos.empty()){
                auto it1 = pos.rbegin();
                l = *it1 + 1;
            }
            else l = 0;
        }
        if (r == 1e9) r = n - 1;
        if (LR.find(r - l + 1) != LR.end()) LR.erase(LR.find(r - l + 1));
        if (LR.find(r - l + 1) == LR.end() && LRc.find(r - l + 1) != LRc.end()) LRc.erase(r - l + 1);
        pos.insert(a[i].se);
        if (x - l > 0){
            LR.insert(x - l);
            LRc.insert(x - l);
        }
        if (r - x > 0){
            LR.insert(r - x);
            LRc.insert(r - x);
        }
        if (LRc.size() == 1){
            if ((int) LR.size() == mx){
                ans = min(ans, a[i].fi);
                continue;
            }
            if ((int) LR.size() > mx){
                mx = LR.size();
                ans = a[i].fi;
            }
        }
    }
    for (int i=0; i<n; ++i) b[i] = a[i].fi;
    sort(b, b + n);
    auto it = lower_bound(b, b + n, ans) - b;
    if (it) ans = b[it - 1] + 1;
    cout << ans << endl;
    return 0;
}
// 8 1 2 7 3 4 8 5 6


#include <bits/stdc++.h>

#define fi first
#define se second
#define p_b push_back
#define m_p make_pair
#define ld long double
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define sqr(x) ((x) * (x))
#define pii pair<int, int>
#define stop system("pause");
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
typedef long long ll;
using namespace std;
template <typename T>
void vout(T x){
    cout << x;
    exit(0);
}
bool f;
ll n, a[210000], k, mstcol, col, mn = 1e9 + 7, mx = 1, c1;
int main()
{
    // Stupid attempt with n^2;
    //
    fast_io
    cin >> n;
    for (int i=0; i<n; ++i) cin >> a[i];
    for (int i=0; i<n; ++i){
        k = a[i];
        f = col = mstcol = c1 = 0;
        for (int j=0; j<=n; ++j){
            if (j == n){
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
                break;
            }
            if (a[j] < k) col++;
            else {
                if (col){
                    if (!mstcol) mstcol = col;
                    else if (mstcol != col) {f = 1, c1 = 0; break;}
                    c1++;
                    col = 0;
                }
            }
        }
        if (!f && mx < c1) mn = k, mx = c1;
        if (!f && mx == c1) mn = min(mn, k);
    }
    vout(mn);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp make_pair
#define CHECK(x) if(!(x)) return false;
#define CHECKRET(x, y) if(!(x)) return (y);
#define SKIP(x) if((x)) continue;
typedef pair<int, int> pii;
typedef unsigned long long hsh_type;
typedef pair<hsh_type, int> fullhash;

const int MAXN = 100010;
pii v[MAXN];
int N;
set<pii> segs;

const int MAXARB = 400010;
int arbSz;
int arbInt[MAXARB];

inline int leftSon(int x) {
  return x * 2 + 1;
}

inline int rightSon(int x) {
  return x * 2 + 2;
}

inline int parent(int x) {
  return (x - 1) >> 1;
}

void update(int pos, int x) {
  pos += arbSz - 1;
  arbInt[pos] += x;
  pos = parent(pos);
  while (pos >= 0) {
    arbInt[pos] = max(arbInt[leftSon(pos)], arbInt[rightSon(pos)]);
    pos = parent(pos);
  }
}

int query(int l, int r) {
  l += arbSz - 1;
  r += arbSz - 1;
  int best = 0;
  while (l <= r) {
    best = max(best, arbInt[l]);
    best = max(best, arbInt[r]);
    l = parent(l + 1);
    r = parent(r - 1);
  }
  return best;
}

void putArray(int l, int r) {
  if (l >= r) return;
  segs.insert(mp(r, l));
  update(r - l, 1);
}

int main() {
  scanf("%d", &N);
  arbSz = 1;
  while (arbSz <= N)
    arbSz *= 2;
  for (int i = 0; i < N; ++i) {
    scanf("%d", &v[i].first);
    v[i].second = i;
  }
  putArray(0, N);
  sort(v, v + N);
  pii best = mp(1, -(v[N - 1].first + 1));
  for (int ii = N - 1; ii >= 1; --ii) {
    int i = v[ii].second;
    pii arr = *segs.upper_bound(mp(i, INT_MAX));
    segs.erase(arr);
    int l = arr.second, r = arr.first;
    update(r - l, -1);
    putArray(l, i);
    putArray(i + 1, r);
    int someF = query(0, N);
    if (someF == segs.size())
      best = max(best,
        mp(someF, -(v[ii - 1].first + 1))
        );
  }
  printf("%d\n", -best.second);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp make_pair
#define CHECK(x) if(!(x)) return false;
#define CHECKRET(x, y) if(!(x)) return (y);
#define SKIP(x) if((x)) continue;
typedef pair<int, int> pii;
typedef unsigned long long hsh_type;
typedef pair<hsh_type, int> fullhash;

const int MAXN = 100010;
pii v[MAXN];
int N;
int f[MAXN];
set<pii> segs;

int putArray(int l, int r) {
  if (l >= r) return -1;
  segs.insert(mp(r, l));
  return ++f[r - l];
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &v[i].first);
    v[i].second = i;
  }
  segs.insert(mp(N, 0));
  ++f[N];
  sort(v, v + N);
  pii best = mp(1, -(v[N - 1].first + 1));
  for (int ii = N - 1; ii >= 1; --ii) {
    int i = v[ii].second;
    pii arr = *segs.upper_bound(mp(i, INT_MAX));
    segs.erase(arr);
    int l = arr.second, r = arr.first;
    --f[r - l];
    int someF = max(putArray(l, i), putArray(i + 1, r));
    if (someF == segs.size())
      best = max(best,
        mp(someF, -(v[ii - 1].first + 1))
        );
  }
  printf("%d\n", -best.second);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp make_pair
#define CHECK(x) if(!(x)) return false;
#define CHECKRET(x, y) if(!(x)) return (y);
#define SKIP(x) if((x)) continue;
typedef pair<int, int> pii;
typedef unsigned long long hsh_type;
typedef pair<hsh_type, int> fullhash;

const int MAXN = 100010;
pii v[MAXN];
int N;
set<pii> segs;

const int MAXARB = 400010;
int arbSz;
int arbInt[MAXN];

inline int leftSon(int x) {
  return x * 2 + 1;
}

inline int rightSon(int x) {
  return x * 2 + 2;
}

inline int parent(int x) {
  return (x - 1) >> 1;
}

void update(int pos, int x) {
  pos += arbSz - 1;
  arbInt[pos] += x;
  pos = parent(pos);
  while (pos >= 0) {
    arbInt[pos] = max(arbInt[leftSon(pos)], arbInt[rightSon(pos)]);
    pos = parent(pos);
  }
}

int query(int l, int r) {
  l += arbSz - 1;
  r += arbSz - 1;
  int best = 0;
  while (l <= r) {
    best = max(best, arbInt[l]);
    best = max(best, arbInt[r]);
    l = parent(l + 1);
    r = parent(r - 1);
  }
  return best;
}

void putArray(int l, int r) {
  if (l >= r) return;
  segs.insert(mp(r, l));
  update(r - l, 1);
}

int main() {
  scanf("%d", &N);
  arbSz = 1;
  while (arbSz <= N)
    arbSz *= 2;
  for (int i = 0; i < N; ++i) {
    scanf("%d", &v[i].first);
    v[i].second = i;
  }
  putArray(0, N);
  sort(v, v + N);
  pii best = mp(1, -(v[N - 1].first + 1));
  for (int ii = N - 1; ii >= 1; --ii) {
    int i = v[ii].second;
    pii arr = *segs.upper_bound(mp(i, INT_MAX));
    segs.erase(arr);
    int l = arr.second, r = arr.first;
    update(r - l, -1);
    putArray(l, i);
    putArray(i + 1, r);
    int someF = query(0, N);
    if (someF == segs.size())
      best = max(best,
        mp(someF, -(v[ii - 1].first + 1))
        );
  }
  printf("%d\n", -best.second);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mp make_pair
#define CHECK(x) if(!(x)) return false;
#define CHECKRET(x, y) if(!(x)) return (y);
#define SKIP(x) if((x)) continue;
typedef pair<int, int> pii;
typedef unsigned long long hsh_type;
typedef pair<hsh_type, int> fullhash;

const int MAXN = 100010;
pii v[MAXN];
int N;
int f[MAXN];
set<pii> segs;

int putArray(int l, int r) {
  if (l >= r) return -1;
  segs.insert(mp(r, l));
  return ++f[r - l];
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &v[i].first);
    v[i].second = i;
  }
  segs.insert(mp(N, 0));
  ++f[N];
  sort(v, v + N);
  pii best = mp(1, -(v[N - 1].first + 1));
  for (int ii = N - 1; ii >= 1; --ii) {
    int i = v[ii].second;
    pii arr = *segs.upper_bound(mp(i, 0));
    segs.erase(arr);
    int l = arr.second, r = arr.first;
    --f[r - l];
    int someF = max(putArray(l, i), putArray(i + 1, r));
    if (someF == segs.size())
      best = max(best,
        mp(someF, -(v[ii - 1].first + 1))
        );
  }
  printf("%d\n", -best.second);
  return 0;
}


#include <bits/stdc++.h>
//#include "rail.h"
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll unsigned long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()

using namespace std;
const long long INF = 1e16;
const long long MOD = 1e15+9;
const int MX=100015;
int a[MX];
int ind[MX];
bool cmp(int x,int y){
    R a[x]>a[y];
}
int seg_t[MX];
int seg_n=1;
int l[MX];
set<int> st;
bool check(){
    int up=*st.lower_bound(0);
    int seg_siz=up-l[up]+1;
    if(seg_t[seg_siz]==seg_n)R 1;
    else R 0;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ind[i]=i;
    }
    st.insert(n-1);
    sort(ind,ind+n,cmp);
    l[n-1]=0;
    seg_t[n]=1;
    int max_n=1,ans=0;
    for(int i=0;i<n;i++){
        int j=ind[i];
        int ri=*st.lower_bound(j);
        int le=l[ri];
//        cout<<le<<" "<<j<<" "<<ri<<endl;
        if(le==ri){
            seg_n--;
            seg_t[1]--;
            st.erase(ri);
        }
        else{
            if(ri==j){
                l[ri-1]=le;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
                st.erase(ri);
                st.insert(ri-1);
            }
            else if(le==j){
                l[ri]=le+1;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
            }
            else{
                l[ri]=j+1;
                seg_t[ri-le+1]--;
                seg_t[ri-j]++;
                seg_t[j-le]++;
                seg_n++;
                l[j-1]=le;
                st.insert(j-1);
            }
        }
        if(check()){
            if(max_n<=seg_n)
                max_n=seg_n,ans=a[j];
        }
    }
//    cout<<max_n<<endl;
    cout<<ans;
}


#include <bits/stdc++.h>
//#include "rail.h"
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll unsigned long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()

using namespace std;
const long long INF = 1e16;
const long long MOD = 1e15+9;
const int MX=100015;
int a[MX];
int ind[MX];
bool cmp(int x,int y){
    R a[x]>a[y];
}
int seg_t[MX];
int seg_n=1;
int l[MX];
set<int> st;
bool check(){
    int up=*st.lower_bound(0);
    int seg_siz=up-l[up]+1;
    if(seg_t[seg_siz]==seg_n)R 1;
    else R 0;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ind[i]=i;
    }
    st.insert(n-1);
    sort(ind,ind+n,cmp);
    l[n-1]=0;
    seg_t[n]=1;
    int max_n=1,ans=a[ind[0]]+1;
    for(int i=0;i<n;i++){
        int j=ind[i];
        int ri=*st.lower_bound(j);
        int le=l[ri];
//        cout<<le<<" "<<j<<" "<<ri<<endl;
        if(le==ri){
            seg_n--;
            seg_t[1]--;
            st.erase(ri);
        }
        else{
            if(ri==j){
                l[ri-1]=le;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
                st.erase(ri);
                st.insert(ri-1);
            }
            else if(le==j){
                l[ri]=le+1;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
            }
            else{
                l[ri]=j+1;
                seg_t[ri-le+1]--;
                seg_t[ri-j]++;
                seg_t[j-le]++;
                seg_n++;
                l[j-1]=le;
                st.insert(j-1);
            }
        }
        if(check()){
            if(max_n<=seg_n)
                max_n=seg_n,ans=a[j];
        }
    }
//    cout<<max_n<<endl;
    cout<<ans;
}


#include <bits/stdc++.h>
//#include "rail.h"
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll unsigned long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()

using namespace std;
const long long INF = 1e16;
const long long MOD = 1e15+9;
const int MX=100015;
int a[MX];
int ind[MX];
bool cmp(int x,int y){
    R a[x]>a[y];
}
int seg_t[MX];
int seg_n=1;
int l[MX];
set<int> st;
bool check(){
    int up=*st.lower_bound(0);
    int seg_siz=up-l[up]+1;
    if(seg_t[seg_siz]==seg_n)R 1;
    else R 0;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ind[i]=i;
    }
    st.insert(n-1);
    sort(ind,ind+n,cmp);
    l[n-1]=0;
    seg_t[n]=1;
    int max_n=1,ans=a[ind[0]]+1;
    for(int i=0;i<n;i++){
        int j=ind[i];
        int ri=*st.lower_bound(j);
        int le=l[ri];
//        cout<<le<<" "<<j<<" "<<ri<<endl;
        if(le==ri){
            seg_n--;
            seg_t[1]--;
            st.erase(ri);
        }
        else{
            if(ri==j){
                l[ri-1]=le;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
                st.erase(ri);
                st.insert(ri-1);
            }
            else if(le==j){
                l[ri]=le+1;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
            }
            else{
                l[ri]=j+1;
                seg_t[ri-le+1]--;
                seg_t[ri-j]++;
                seg_t[j-le]++;
                seg_n++;
                l[j-1]=le;
                st.insert(j-1);
            }
        }
        if(check()){
            if(max_n<=seg_n)
                max_n=seg_n,ans=a[j-1]+1;
        }
    }
//    cout<<max_n<<endl;
    cout<<ans;
}


#include <bits/stdc++.h>
//#include "rail.h"
#define F first
#define S second
#define P push
#define pb push_back
#define MEM(dp,i) memset(dp,i,sizeof(dp))
#define W while
#define R return
#define C continue
#define SI size()
#define ll unsigned long long
#define ld long double
#define pll pair<ll,ll>
#define pii pair<int,int>
#define SF(x) scanf("%Id",&x)
#define SF2(x,y) scanf("%Id%Id",&x,&y)
#define SF3(x,y,z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define SF4(x,y,z,o) scanf("%I64d%I64d%I64d%I64d",&x,&y,&z,&o)
#define all(v) v.begin(),v.end()

using namespace std;
const long long INF = 1e16;
const long long MOD = 1e15+9;
const int MX=100015;
int a[MX];
int ind[MX];
bool cmp(int x,int y){
    R a[x]>a[y];
}
int seg_t[MX];
int seg_n=1;
int l[MX];
set<int> st;
bool check(){
    int up=*st.lower_bound(0);
    int seg_siz=up-l[up]+1;
    if(seg_t[seg_siz]==seg_n)R 1;
    else R 0;
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        ind[i]=i;
    }
    st.insert(n-1);
    sort(ind,ind+n,cmp);
    l[n-1]=0;
    seg_t[n]=1;
    int max_n=1,ans=a[ind[0]]+1;
    for(int i=0;i<n;i++){
        int j=ind[i];
        int ri=*st.lower_bound(j);
        int le=l[ri];
//        cout<<le<<" "<<j<<" "<<ri<<endl;
        if(le==ri){
            seg_n--;
            seg_t[1]--;
            st.erase(ri);
        }
        else{
            if(ri==j){
                l[ri-1]=le;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
                st.erase(ri);
                st.insert(ri-1);
            }
            else if(le==j){
                l[ri]=le+1;
                seg_t[ri-le+1]--;
                seg_t[ri-le]++;
            }
            else{
                l[ri]=j+1;
                seg_t[ri-le+1]--;
                seg_t[ri-j]++;
                seg_t[j-le]++;
                seg_n++;
                l[j-1]=le;
                st.insert(j-1);
            }
        }
        if(check()){
            if(max_n<=seg_n)
                max_n=seg_n,ans=a[ind[i+1]]+1;
        }
    }
//    cout<<max_n<<endl;
    cout<<ans;
}


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

set<int> diff;
map<int, int> g, dp;
priority_queue<int> pq;
set< pair<int, int> > s;

int main () {
	int ans = 0, total = 1;
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x; scanf("%d", &x);
		pq.push(x), g[x] = i, ans = max(ans, x + 1);
	}
	dp[n] = 1;
	diff.insert(n);
	s.insert(make_pair(0, n - 1));
	for (; int(pq.size()); pq.pop()) {
		int i = pq.top();
		int j = g[i];
		auto it = s.lower_bound(make_pair(j, 0));
		if (it == s.end() or j < it->first)
			--it;
		pair<int, int> kappa = make_pair(it->first, it->second); s.erase(it);
		if (!--dp[kappa.second - kappa.first + 1])
			diff.erase(kappa.second - kappa.first + 1);
		if (j - 1 >= kappa.first)
			s.insert(make_pair(kappa.first, j - 1)), ++dp[(j - 1) - kappa.first + 1], diff.insert((j - 1) - kappa.first + 1);
		if (j + 1 < kappa.second)
			s.insert(make_pair(j + 1, kappa.second)), ++dp[kappa.second - (j + 1) + 1], diff.insert(kappa.second - (j + 1) + 1);
		if (int(diff.size()) == 1 and int(s.size()) >= total)
			total = int(s.size()), ans = i + 1;
	}
	return printf("%d\n", ans), 0;
}


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

set<int> diff;
map<int, int> g, dp;
priority_queue<int> pq;
set< pair<int, int> > s;

int main () {
	int ans = 0, total = 1;
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x; scanf("%d", &x);
		pq.push(x), g[x] = i, ans = max(ans, x + 1);
	}
	dp[n] = 1, diff.insert(n), s.insert(make_pair(0, n - 1));
	for (; int(pq.size()); pq.pop()) {
		int i = pq.top();
		if (int(diff.size()) < 2 and int(s.size()) >= total)
			total = int(s.size()), ans = i + 1;
		int j = g[i];
		auto it = s.lower_bound(make_pair(j, 0));
		if (it == s.end() or j < it->first)
			--it;
		pair<int, int> kappa = make_pair(it->first, it->second); s.erase(it);
		if (!--dp[kappa.second - kappa.first + 1])
			diff.erase(kappa.second - kappa.first + 1);
		if (j - 1 >= kappa.first)
			s.insert(make_pair(kappa.first, j - 1)), ++dp[(j - 1) - kappa.first + 1], diff.insert((j - 1) - kappa.first + 1);
		if (j + 1 <= kappa.second)
			s.insert(make_pair(j + 1, kappa.second)), ++dp[kappa.second - (j + 1) + 1], diff.insert(kappa.second - (j + 1) + 1);
		if (int(diff.size()) < 2 and int(s.size()) >= total)
			total = int(s.size()), ans = i;
	}
	return printf("%d\n", ans), 0;
}


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

set<int> diff;
map<int, int> g, dp;
priority_queue<int> pq;
set< pair<int, int> > s;

int main () {
	int ans = 0, total = 1;
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x; scanf("%d", &x);
		pq.push(x), g[x] = i, ans = max(ans, x + 1);
	}
	dp[n] = 1, diff.insert(n), s.insert(make_pair(0, n - 1));
	for (; int(pq.size()); pq.pop()) {
		int i = pq.top();
		int j = g[i];
		auto it = s.lower_bound(make_pair(j, 0));
		if (it == s.end() or j < it->first)
			--it;
		pair<int, int> kappa = make_pair(it->first, it->second); s.erase(it);
		if (!--dp[kappa.second - kappa.first + 1])
			diff.erase(kappa.second - kappa.first + 1);
		if (j - 1 >= kappa.first)
			s.insert(make_pair(kappa.first, j - 1)), ++dp[(j - 1) - kappa.first + 1], diff.insert((j - 1) - kappa.first + 1);
		if (j + 1 <= kappa.second)
			s.insert(make_pair(j + 1, kappa.second)), ++dp[kappa.second - (j + 1) + 1], diff.insert(kappa.second - (j + 1) + 1);
		if (int(diff.size()) < 2 and int(s.size()) >= total)
			total = int(s.size()), ans = i;
	}
	return printf("%d\n", ans), 0;
}


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

set<int> diff;
map<int, int> g, dp;
priority_queue<int> pq;
set< pair<int, int> > s;

int main () {
	int ans = 0, total = 1;
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
		int x; scanf("%d", &x);
		pq.push(x), g[x] = i, ans = max(ans, x + 1);
	}
	dp[n] = 1;
	diff.insert(n);
	s.insert(make_pair(0, n - 1));
	for (; int(pq.size()); pq.pop()) {
		int i = pq.top();
		int j = g[i];
		auto it = s.lower_bound(make_pair(j, 0));
		if (it == s.end() or j < it->first)
			--it;
		pair<int, int> kappa = make_pair(it->first, it->second); s.erase(it);
		if (!--dp[kappa.second - kappa.first + 1])
			diff.erase(kappa.second - kappa.first + 1);
		if (j - 1 >= kappa.first)
			s.insert(make_pair(kappa.first, j - 1)), ++dp[(j - 1) - kappa.first + 1], diff.insert((j - 1) - kappa.first + 1);
		if (j + 1 < kappa.second)
			s.insert(make_pair(j + 1, kappa.second)), ++dp[kappa.second - (j + 1) + 1], diff.insert(kappa.second - (j + 1) + 1);
		if (int(diff.size()) == 1 and int(s.size()) >= total)
			total = int(s.size()), ans = i;
	}
	return printf("%d\n", ans), 0;
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

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

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

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

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

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

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

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    int skip = 0;

    for (int i = 1; i < N; i++) {

        if (skip == 0 && CSZ > RSZ) {
            bool ok = true;

            int mnv = 1 << 25, mxd = 0;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                mnv = min(mnv, sz[*x]);
                mxd = max(mxd, sz[*x]);

                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }

            skip = mxn - mnd + 1;
        }

        skip--;

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}


#include <bits/stdc++.h>

#define f first
#define s second
#define mp make_pair
#define pb push_back

#define left(x) ((x) << 1)
#define right(x) ((x) << 1 | 1)
#define mid(x, y) ((x) + (y) >> 1)

using namespace std;

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

const int maxk = 17 + 1;
const int maxn = 1 * 1e5 + 17;

int N, R, RSZ, CSZ;
int inv[maxn];
int sz[maxn], parent[maxn];
set<int> dist_parents;
vector<int> v, val, srt;

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

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

    if (x == y) return;

    if (sz[x] < sz[y]) swap(x, y);

    dist_parents.erase(y);

    sz[x] += sz[y];
    parent[y] = x;
}

int main()
{
    ios_base::sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N; i++) {
        int x; cin >> x;
        val.pb(x);
        srt.pb(x);
    }

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

    R = *srt.begin() + 1;
    RSZ = 0;

    for (int i = 0; i < N; i++) {
        int x = lower_bound(srt.begin(), srt.end(), val[i]) - srt.begin();
        v.pb(x);
        inv[x] = i;
    }

    for (int i = 0; i < N; i++) parent[i] = i, sz[i] = 1;

    CSZ = 1;
    dist_parents.insert(inv[0]);

    int skip = 1;

    for (int i = 1; i < N; i++) {

        if (skip == i && CSZ > RSZ) {
            bool ok = true;

            int mnv = 1 << 25, mxd = 0;

            for (auto x = dist_parents.begin(); x != dist_parents.end(); x++) {
                mnv = min(mnv, sz[*x]);
                mxd = max(mxd, sz[*x]);

                if (sz[*x] != sz[*dist_parents.begin()]) {
                    ok = false;
                    break;
                }
            }

            if (ok) {
                RSZ = CSZ;
                R = val[inv[i - 1]] + 1;
            }

            skip = mxd - mnv + 1;
        }

        CSZ++; dist_parents.insert(inv[i]);

        if (inv[i] - 1 >= 0 && v[inv[i]] > v[inv[i] - 1]) {
            CSZ--;
            merge(inv[i], inv[i] - 1);
        }

        if (inv[i] + 1 < N && v[inv[i]] > v[inv[i] + 1]) {
            CSZ--;
            merge(inv[i], inv[i] + 1);
        }
    }

    return !printf("%d\n", R);
}