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>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pll pair<ll,ll>
#define pb push_back
#define F first
#define S second
#define I insert
#define vll vector<ll>
#define vpll vector<pll>
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()
#define endl '\n'

ll power(ll a, ll b, ll mod){ ll c = 1; while(b>0){ if(b%2) c*=a, c%=mod; b/=2; a*=a; a%=mod; } return c; }
 
const ll N = (1e5) + 10;
const ll M = 320;
const ll Mod = 1e9 + 7;
ll ar[N],br[N];
vll u;

ll go(ll a, ll b){
	if(a == b) return 0;
	ll mid = (a+b)/2;
	ll ans = go(a,mid)+go(mid+1,b);
	u.clear();
	ll i,j;
	i = a; j = mid+1;
	while(i <= mid && j <= b){
		while(i<=mid && br[i] <= br[j]) u.pb(br[i++]), ans += j-(mid+1);
		if(i > mid) break;
		while(j<=b && br[j] < br[i]) u.pb(br[j++]);
	}
	while(i<=mid) u.pb(br[i++]), ans += j-(mid+1);
	while(j<=b) u.pb(br[j++]);
	for(i=a;i<=b;i++)
		br[i] = u[i-a];
	return ans;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout<<setprecision(25);
    ll n,hi,lo,mid,a,b,c,d,i,j,k;
    cin>>n; vll v;
    for(i=1;i<=n;i++) cin>>ar[i], v.pb(ar[i]);
    ll amt = (n*(n+1))/2;
	ll inv,ans;
	sort(all(v)); v.resize(unique(all(v))-v.begin());
	lo = 0;
	hi = sz(v)-1;
    while(hi >= lo){
    	mid = (hi+lo)/2;
    	for(i=1;i<=n;i++) br[i] = ((ar[i] >= v[mid]) ? 1 : -1), br[i] += br[i-1];
		inv = amt - go(1,n);
    	if(2*inv >= amt){
    		ans = mid;
    		lo = mid+1;
    	}
    	else{
    		hi = mid-1;
    	}
    }
    cout<<v[ans]<<endl;
    return 0;
}