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;

#define LL long long
#define REP(i, n) for(LL i = 0; i < (LL)(n); i++)
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ALL(x) (x).begin(),(x).end()

template<typename T>
class SegmentTree {
private:
    int N;
    vector<T> v;
    vector<tuple<int, int, T>> heap;
    function<T(T, T)> merge;

    T init(int l, LL r, int i) {
        if(l == r) {
            return 0;
        }
        if(r - l == 1) {
            T value = v[l];
            heap[i] = make_tuple(l, r, value);
            return value;
        }
        T value = merge(init(l, (l + r)/2, i*2), init((l + r)/2, r, i*2 + 1));
        heap[i] = make_tuple(l, r, value);
        return value;
    }

public:
    SegmentTree() {}

    SegmentTree(vector<T> s, function<T(T, T)> f) {
        merge = f;
        int n = s.size();
        for(N = 1;N < n;N *= 2);
        heap.resize(2*N, make_tuple(0, 0, 0));
        v.resize(N, 0);
        copy(ALL(s), v.begin());
        init(0, N, 1);
    }

    SegmentTree(vector<T> s) : SegmentTree(s, [](T x, T y){return x + y;}) {}

    void update(int index, T value) {
        int l = 0;
        int r = N;
        int i = 1;
        for(;1 < r - l;) {
            int mid = (l + r)/2;
            if(index < mid) {
                r = mid;
                i *= 2;
            }else{
                l = mid;
                i = i*2 + 1;
            }
        }
        heap[i] = make_tuple(l, r, value);

        for(i /= 2;0 < i;i /= 2) {
            int l, r;
            tie(l, r, ignore) = heap[i];
            T lv, rv;
            tie(ignore, ignore, lv);
            tie(ignore, ignore, rv);
            T v = merge(lv, rv);
            heap[i] = make_tuple(l, r, v);
        }
    }

    T segment(int l, int r, int i=1) {
        int left = get<0>(heap[i]);
        int right = get<1>(heap[i]);
        int mid = (left + right)/2;
        T value = get<2>(heap[i]);

        if(l == r) {
            return 0;
        }
        if(left == right) {
            return 0;
        }
        if(left == l && right == r) {
            return value;
        }
        if(r < mid) {
            return segment(l, r, i*2);
        }
        if(mid < l) {
            return segment(l, r, i*2 + 1);
        }
        return merge(segment(l, mid, i*2), segment(mid, r, i*2 + 1));
    }
};

bool check(LL N, vector<LL> a, LL p) {
    vector<LL> t(a);
    REP(i, N) {
        t[i] = p <= a[i] ? 1 : -1;
    }
    vector<LL> s(N + 1, 0);
    REP(i, N) {
        s[i + 1] = s[i] + t[i];
    }
    vector<LL> v(2*N + 1, 0);
    SegmentTree<LL> seg(v);
    LL ans = 0;
    REP(i, N + 1) {
        LL j = s[i] + N;
        ans += seg.segment(0, j + 1);
        LL y = seg.segment(j, j + 1);
        seg.update(j, y + 1);
    }
    return (N + 1)*N/4 <= ans;
}

int main() {
    int N;
    cin >> N;
    vector<LL> a(N);
    REP(i, N) {
        cin >> a[i];
    }
    vector<LL> com(a);
    sort(ALL(com));

    int left = 0;
    int right = N - 1;
    if(check(N, a, com[right])) {
        cout << com[right] << endl;
        return 0;
    }

    for(;1 < right - left;) {
        int mid = (left + right)/2;
        bool c = check(N, a, com[mid]);
        if(c) {
            left = mid;
        }else{
            right = mid;
        }
    }
    cout << com[left] << endl;
    return 0;
}