Problem:
Score : 1300 points

Problem Statement

We have a pyramid with N steps, built with blocks.
The steps are numbered 1 through N from top to bottom.
For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally.
The pyramid is built so that the blocks at the centers of the steps are aligned vertically.

A pyramid with N=4 steps

Snuke wrote a permutation of (1, 2, ..., 2N-1) into the blocks of step N.
Then, he wrote integers into all remaining blocks, under the following rule:

The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.

Writing integers into the blocks

Afterwards, he erased all integers written into the blocks.
Now, he only remembers that the permutation written into the blocks of step N was (a_1, a_2, ..., a_{2N-1}).

Find the integer written into the block of step 1.

Constraints

2≤N≤10^5

(a_1, a_2, ..., a_{2N-1}) is a permutation of (1, 2, ..., 2N-1).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{2N-1}

Output

Print the integer written into the block of step 1.

Sample Input 1

4
1 6 3 7 4 5 2

Sample Output 1

4

This case corresponds to the figure in the problem statement.

Sample Input 2

2
1 2 3

Sample Output 2

2

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

#define MP make_pair
#define PB push_back
#define FF first
#define SS second

#define FORN(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD(i, n) for (int i = (int)(n) - 1; i >= 0; i--)

#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR0(A,n) { cout << #A << " = "; FORN(_,n) cout << A[_] << ' '; cout << endl; }

#define MOD 1000000007
#define INF 2000000000

int GLL(LL& x) {
    return scanf("%lld", &x);
}

int GI(int& x) {
    return scanf("%d", &x);
}

const int MAXN = 100010;
int a[2 * MAXN];

int n, res;

void binSearchHelper(bool topSquare, int& lo, int& hi, int& mid) {
    if (topSquare) {
        res = mid;
        lo = mid + 1;
    }
    else {
        hi = mid - 1;
    }
}

int main() {
    GI(n);

    FOR1(i, 2*n-1) {
        GI(a[i]);
    }

    int lo = 1; int hi = 2*n-1; res = -1;

    while (lo <= hi) {
        int mid = (lo + hi) / 2; // is the answer >= mid?

        if ((a[n] >= mid && a[n-1] >= mid) || (a[n] >= mid && a[n+1] >= mid)) {
            lo = mid + 1; res = mid;
            continue;
        }
        else if ((a[n] < mid && a[n-1] < mid) || (a[n] < mid && a[n+1] < mid)) {
            hi = mid - 1;
            continue;
        }

        int l = n, r = n;

        while (l-1 >= 1 && (a[l] >= mid) != (a[l-1] >= mid)) {
            l--;
        }
        while (r+1 <= 2*n-1 && (a[r] >= mid) != (a[r+1] >= mid)) {
            r++;
        }

        bool bl = (a[l] >= mid);
        bool br = (a[r] >= mid);

        if (l > 1 && r < 2*n-1) {
            if (bl == br) {
                binSearchHelper(bl, lo, hi, mid);
            }
            else if (r - n < n - l) { // r is closer
                binSearchHelper(br, lo, hi, mid);
            }
            else { // l is closer
                binSearchHelper(bl, lo, hi, mid);
            }
        }
        else if (l == 1) {
            binSearchHelper(br, lo, hi, mid);
        }
        else if (r == 2*n-1) {
            binSearchHelper(bl, lo, hi, mid);
        }
        else {
            binSearchHelper(bl, lo, hi, mid);
        }
    }

    printf("%d\n", res);
    
    return 0;
}