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 <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <string>
#include <string.h>
#include <tuple>
#include <vector>

#define REP(i,x) for(int i{ 0 }; i < (int)(x); i++)
#define REPC(i,x) for(int i{ 0 }; i <= (int)(x); i++)
#define RREP(i,x) for(int i{ (int)(x) - 1 }; i >= 0 ;i--)
#define RREPC(i,x) for(int i{ (int)(x)}; i >= 0; i--)
#define REP1O(i,x) for(int i{ 1 }; i < (int)(x); i++)
#define REP1C(i,x) for(int i{ 1 }; i <= (int)(x); i++)

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

using namespace std;

typedef int64_t ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tupiii;
typedef tuple<ll, ll, ll> tuplll;

const int INTMAX = 2147483647;
const ll LLMAX = 9223372036854775807;
const int MOD = 1000000007;

template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
inline void swap(ll& a, ll& b) { a ^= b; b ^= a; a ^= b; }
inline void swap(int& a, int& b) { a ^= b; b ^= a; a ^= b; }

template <class T>
struct BIT {
private:
	vector<T> data;
	int maxid;

public:
	BIT(int n);
	BIT();
	void init(int n);

	void add(int idx, T val);

	// sum up to idx (close-end)
	T sum(int idx);

	// sum for [l, r) (1-indexed)
	T sum(int left, int right);

	T operator[](int i);
};

int n;
vi va;
ll n_totsect;

void init()
{
	cin >> n;
	n_totsect = (ll)n * ((ll)n + 1) / 2;
	va.resize(n);
	REP(i, n)
		cin >> va[i];
}

bool check(int x)
{
	BIT<ll> bit(2 * n);
	ll n_sect = 0;
	int sum = 0;

	bit.add(n, 1);

	REP(i, n)
	{
		sum += (va[i] <= x) ? 1 : -1;
		n_sect += bit.sum(sum + n);
		bit.add(sum + n, 1);
	}

	return n_sect * 2 >= n_totsect;
}

int main()
{
	init();

	int ng = 0, ok = 1 << 30;
	while (ok - ng > 1)
	{
		int md = (ok + ng) / 2;
		if (check(md))
			ok = md;
		else
			ng = md;
	}

	cout << ok << endl;
}

////////// 1-d BIT implementation //////////

// default constructor
template <class T> BIT<T>::BIT() : BIT(0) {}

// constructor. input is max-value of the data you want to deal
template <class T> BIT<T>::BIT(int n) : data(n + 1, (T)0), maxid{ n } {}

// initializer
template <class T> void BIT<T>::init(int n) { data.resize(n + 1, (T)0); this->maxid = n; }

template <class T> void BIT<T>::add(int idx, T val) { for (++idx; idx <= maxid; idx += idx & (-idx)) data[idx] += val; }

// sum up to idx (close-end)
template <class T> T BIT<T>::sum(int idx) { T ret{ 0 }; for (; idx > 0; idx -= idx & (-idx)) ret += data[idx]; return ret; }

// sum for [l, r)
template <class T> T BIT<T>::sum(int left, int right) { return sum(right - 1) - sum(left - 1); }

template <class T> T BIT<T>::operator[](int i) { return sum(i) - sum(i - 1); }

////////// END of 1-d BIT implementation //////////