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: Time Limit Exceeded)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define PI 				acos(-1)
#define mp				make_pair
#define pb				push_back
#define all(a)			(a).begin(), (a).end()
#define sz(a)			a.size()
#define srt(a)			sort(all(a))
#define mem(a, h)		memset(a, (h), sizeof(a))
#define f 				first
#define s 				second
#define MOD1			998244353
#define fore(i, b, e)	for(int i = b; i < e; i++)
#define forg(i, b, e, m)	for(int i = b; i < e; i+=m)
int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}

using namespace std;
using namespace __gnu_pbds;

typedef long long 		ll;
typedef unsigned long long 		ull;
typedef pair<int, int>  ii;
typedef vector<int>     vi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int tam = 100010;
const int MOD = 1e9+7;
const double EPS = 1e-9;
int t[400000];
void update(int b, int e, int node, int pos, int val)
{
	if(b == e){
		t[node] += val;
		return;
	}
	int mid = (b + e) / 2, l = node * 2 + 1, r = l + 1;
	if(mid < pos)
		update(mid + 1, e, r, pos, val);
	else 
		update(b, mid, l, pos, val);
	t[node] = t[l] + t[r];
}
int query(int b, int e, int node, int val)
{
	if(b == e)
		return e;
	int mid = (b + e) / 2, l = node * 2 + 1, r = l + 1;
	if(t[l] < val)
		return query(mid + 1, e, r, val - t[l]);
	return query(b, mid, l, val);
}
int main()
{
	//ios::sync_with_stdio(0); cin.tie(0);
	//freopen("qwe.txt", "r", stdin);
	//freopen("asd.txt", "w", stdout);
	int n;
	cin>>n;
	int ar[n], val[n], con[n];
	mem(con, 0);
	mem(val, 0);
	map<int,int> ma;
	int co = 0;
	vector<int> v(n);
	fore(i, 0, n)
	{
		cin>>ar[i];
		ma[ar[i]]++;
		v[i] = i+1;
	}
	map<int,int> me;
	for(map<int,int>::iterator it = ma.begin(); it != ma.end(); it++)
	{
		val[co] = it->f;
		me[it->f] = co;
		co++;
	}
	//cout<<co;
	fore(i, 0, n)
		ar[i] = me[ar[i]];
	random_shuffle(v.begin(), v.end());
	fore(i, 0, min(500, (int)v.size()))
	{
		//cout<<v[i]<<'\n';
		mem(t, 0);
		fore(j, 0, v[i])
		{
			//cout<<ar[j]<<'\n';
			update(0, co-1, 0, ar[j], 1);
		}
		con[query(0, co-1, 0, v[i] / 2 + 1)]++;
		fore(j, v[i], n)
		{
			update(0, co-1, 0, ar[j], 1);
			update(0, co-1, 0, ar[j - v[i]], -1);
			con[query(0, co-1, 0, v[i] / 2 + 1)]++;
			//cout<<query(0, co-1, 0, v[i] / 2 + 1)<<' ';
		}
		//cout<<'\n';
	}
	int valma, may = 0;
	int tot = 0;
	fore(i, 0, co)
	{
		tot += con[i];
	}
	ll acu = 0;
	fore(i, 0, co)
	{
		acu += con[i];
		if(acu >= tot / 2 + 1)
		{
			cout<<val[i]<<'\n';
			return 0;
		}
	}
	//cout<<valma<<'\n';
	return 0;
}

// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2