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 Python (3.4.3) (judge verdict: Time Limit Exceeded)

# -*- coding: utf-8 -*-
from sys import stdin
import math
import numpy as np

d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


class SegmentTree(object):
    """
    区分和をSegmentTreeで求める

    Prameters
    ---------
    a_list : list
        対象の配列

    Attributes
    ----------
    n_tree : int
        len(a_list) を超える最小の2べきの数

    tree : list, shape = [2*n_tree - 1]
        セグメント木

    参考URL:
    https://www.slideshare.net/iwiwi/ss-3578491
    http://tsutaj.hatenablog.com/entry/2017/03/29/204841
    """
    def __init__(self, a_list):
        self.n_tree = 2 ** math.ceil(math.log2(len(a_list)))
        self.tree = self.initialize_tree(self.n_tree, a_list)

    def initialize_tree(self, n, a_list):
        """ tree initialization """
        tree = [0 for _ in range(2*n - 1)]
        for i in range(len(a_list)):
            tree[n + i - 1] = a_list[i]
        for i in range(n-2, -1, -1):
            tree[i] = tree[2*i + 1] + tree[2*i + 2]
        return tree

    def update(self, i):
        """ update a_i += 1 """
        idx = self.n_tree + i - 1
        self.tree[idx] += 1
        while idx > 0:
            idx = (idx - 1) // 2
            self.tree[idx] = self.tree[idx*2 + 1] + self.tree[idx*2 + 2]

    def query(self, a, b, k, left, right):
        """
        Get summation value of range [a, b)
        Usage : query(a, b, 0, 0, n_tree)
        """
        if (right <= a) or (left >= b):
            return 0
        elif (left >= a) and (right <= b):
            return self.tree[k]
        else:
            val_left = self.query(a, b, k*2 + 1, left, (left + right) // 2)
            val_right = self.query(a, b, k*2 + 2, (left + right) // 2, right)
            return val_left + val_right


def calc_inversion_num(x, a_list):
    n_a = len(a_list)
    transformed = np.zeros(n_a)
    transformed[a_list < x] = -1
    transformed[transformed != -1] = 1

    cum_t = transformed.cumsum()
    filtered_t = list(sorted(set(cum_t)))
    st = SegmentTree([0] * len(filtered_t))

    inv = (transformed == -1).sum()
    for i, c in enumerate(cum_t):
        temp_inv = st.query(0, filtered_t.index(c)+1, 0, 0, st.n_tree)
        st.update(filtered_t.index(c))
        inv += i - temp_inv

    return inv


N = d_in()
a_list = ds_in()
a_list = np.array(a_list)
sorted_a = np.sort(a_list)

search_range = [0, N]  # range [0, N)
while search_range[1] - search_range[0] > 1:
    mid = (search_range[0] + search_range[1]) // 2
    inv = calc_inversion_num(sorted_a[mid], a_list)
    if (N * (N+1) / 2 - inv) < math.ceil(N * (N+1) / 4):
        search_range[1] = mid
    else:
        search_range[0] = mid

print(sorted_a[search_range[0]])