Problem:
Score : 700 points

Problem Statement

We will define the median of a sequence b of length M, as follows:

Let b' be the sequence obtained by sorting b in non-decreasing order. Then, the value of the (M / 2 + 1)-th element of b' is the median of b. Here, / is integer division, rounding down.

For example, the median of (10, 30, 20) is 20; the median of (10, 30, 20, 40) is 30; the median of (10, 10, 10, 20, 30) is 10.

Snuke comes up with the following problem.

You are given a sequence a of length N.
For each pair (l, r) (1 \leq l \leq r \leq N), let m_{l, r} be the median of the contiguous subsequence (a_l, a_{l + 1}, ..., a_r) of a.
We will list m_{l, r} for all pairs (l, r) to create a new sequence m.
Find the median of m.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the median of m.

Sample Input 1

3
10 30 20

Sample Output 1

30

The median of each contiguous subsequence of a is as follows:

The median of (10) is 10.

The median of (30) is 30.

The median of (20) is 20.

The median of (10, 30) is 30.

The median of (30, 20) is 30.

The median of (10, 30, 20) is 20.

Thus, m = (10, 30, 20, 30, 30, 20) and the median of m is 30.

Sample Input 2

1
10

Sample Output 2

10

Sample Input 3

10
5 9 5 9 8 9 3 5 4 3

Sample Output 3

8

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

#include <bits/stdc++.h>

#define rep2(x,fr,to) for(int (x)=(fr);(x)<(to);(x)++)
#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)
#define repr(x,fr,to) for(int (x)=(fr);(x)>=(to);(x)--)
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii;


const int mod = (int)1e9 + 7;
void dbg(){ cerr << "\n"; } template <typename T,typename ...T2> void dbg(const T& fst, const T2&...rst){ cerr << fst << ": "; dbg(rst...); }


int main()
{
		//cin.tie(0); ios_base::sync_with_stdio(false);
	int n;
	cin >>n;
	vector<ll> a(n);
	rep(i,n) cin >>a[i];
	if(n==1){cout <<a[0] <<"\n"; return 0;}
	if(n==2){cout <<a[1] <<"\n"; return 0;}
	
	vector<ll> ct(n); ct[0]=1;ct[n-1]=2;
	rep2(i, 1, n){
		if(ct[i-1] +2 <=n) ct[i] = ct[i-1] +2;
		else break;
	}
	repr(i,n-1,0){
		if(ct[i] +2 <=n) ct[i-1] = ct[i] +2;
		else break;
	}
	//for(auto x : ct) printf("%d, ",x); puts("");
	int sm = 0;
	rep(i,n) sm += ct[i];
	vector<VI> v(n);
	rep(i,n) v[i] = VI{(int)a[i], i, ct[i]};
	sort(all(v));
	//for(auto x :v) printf("%d:%d ",x.first,x.second); puts("");
	sm /=2;
	sm++;
	
	
	rep(i, n){
		if(v[i][2] >= sm){
			cout << a[v[i][1]] <<"\n";
			return 0;
		}else{
			sm -=v[i][2];
		}
	}
	
	
	return 0;
}