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 <iostream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <limits>
#include <algorithm>
#include <array>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <unordered_map>
#include <random>
#include <cmath>
#include <complex>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

const int INFL = (int)1e9;
const ll INFLL = (ll)1e18;
const double INFD = numeric_limits<double>::infinity();
const double PI = 3.14159265358979323846;
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loopll(i, n) for(ll i = 0; i < (ll)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopll1(i, n) for(ll i = 1; i <= (ll)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Looprll(i, n) for(ll i = (ll)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define Looprll1(i, n) for(ll i = (ll)n; i >= 1; i--)
#define Loopitr(itr, container) for(auto itr = container.begin(); itr != container.end(); itr++)
#define printv(vector) Loop(i, vector.size()) { cout << vector[i] << " "; } cout << endl;
#define printmx(matrix) Loop(i, matrix.size()) { Loop(j, matrix[i].size()) { cout << matrix[i][j] << " "; } cout << endl; }
#define rndf(d) (ll)((double)(d) + (d >= 0 ? 0.5 : -0.5))
#define floorsqrt(x) ((ll)sqrt((double)x) + ((ll)sqrt((double)x) * (ll)sqrt((double)x) <= (ll)(x) ? 0 : -1))
#define ceilsqrt(x) ((ll)sqrt((double)x) + ((ll)x <= (ll)sqrt((double)x) * (ll)sqrt((double)x) ? 0 : 1))
#define rnddiv(a, b) ((ll)(a) / (ll)(b) + ((ll)(a) % (ll)(b) * 2 >= (ll)(b) ? 1 : 0))
#define ceildiv(a, b) ((ll)(a) / (ll)(b) + ((ll)(a) % (ll)(b) == 0 ? 0 : 1))
#define quickio() ios::sync_with_stdio(false); cin.tie(0);
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)

/*******************************************************/

class Binary_Indexed_Tree {
private:
	vll bits;
	int n;
public:
	Binary_Indexed_Tree(vll a) {
		n = (int)pow(2, ceil(log2(a.size())));
		bits = vll(n, 0);
		Loop(i, a.size()) add(i, a[i]);
	}
	void add(int k, ll x) {
		for (int index = k; index < n; index |= index + 1) {
			bits[index] += x;
		}
	}
	// note: sum of [s, t)
	ll sum(int s, int t) {
		ll ret = 0;
		for (int index = t - 1; index >= 0; index = (index & (index + 1)) - 1) {
			ret += bits[index];
		}
		for (int index = s - 1; index >= 0; index = (index & (index + 1)) - 1) {
			ret -= bits[index];
		}
		return ret;
	}
};

// solve the number of pair(i, j) such that a[i] > a[j] (i < j) 
ll solve_inversion_number(const vll &a) {
	int n = a.size();
	map<ll, int> mp;
	Loop(i, n) mp[a[i]] = 1;
	int converted = 0;
	Loopitr(itr, mp) itr->second = converted++;
	vi b(n);
	Loop(i, n) b[i] = mp[a[i]];
	Binary_Indexed_Tree bit(vll(n, 0));
	ll ret = 0;
	Loop(i, n) {
		ret += bit.sum(b[i] + 1, n);
		bit.add(b[i], 1);
	}
	return ret;
}

template<typename argv_t>
class Binary_Search {
private:
	argv_t argv;
	bool(*evalfunc)(ll, argv_t);
	ll search(ll l, ll r) {
		if (abs(r - l) == 1) {
			if (!evalfunc(l, argv)) return r;
			else return l;
		}
		ll m = (l + r) / 2;
		if (!evalfunc(m, argv)) return search(m, r);
		else return search(l, m);
	}
public:
	ll result;
	// range = [l, r), return first value causing "t" in evalfunc that returns l->[f,...,f,t,...,t)->r
	Binary_Search(pair<ll, ll> range, argv_t argv, bool(*evalfunc)(ll, argv_t)) {
		Binary_Search::argv = argv;
		Binary_Search::evalfunc = evalfunc;
		result = search(range.first, range.second);
	}
};

bool evalfunc(ll val, vll a) {
	int n = a.size();
	ll cnt = 0;
	Loop(i, n) {
		if (a[i] >= val) {
			a[i] = 1;
			cnt++;
		}
		else a[i] = -1;
	}
	vll s(n);
	s[0] = a[0];
	Loop1(i, n - 1) {
		s[i] = s[i - 1] + a[i];
	}
	cnt += (ll)n * (n - 1) / 2 - solve_inversion_number(s);
	if (cnt >= ceildiv((ll)n * (n - 1) / 2 + n, 2)) return false;
	else return true;
}

int main() {
	int n; cin >> n;
	vll a(n); Loop(i, n) cin >> a[i];
	Binary_Search<vll> bs({0, (ll)1e9 + 1}, a, evalfunc);
	sort(a.begin(), a.end());
	auto itr = lower_bound(a.begin(), a.end(), bs.result);
	itr--;
	cout << *itr << endl;
}