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: Accepted)

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>

using namespace std;

#define REP(i,n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;

/// BIT from 蟻本を参考に
// [1, n]
const int MAX_N = 1<<18;
vector<ll> bit(MAX_N + 1);
ll n;
 
// sum(a[1]..a[n])
ll sum(int i, const int offset) {
  i += offset;
  ll s = 0;
  while(i > 0) {
    s += bit[i];
    i -= i & -i; 
  }
  return s;
}
 
// a[i] += x
void add(int i, const int x, const int offset) {
  i += offset;
  while(i < bit.size()) {
    bit[i] += x;
    //    cout << "A: " << i << " " << bit[i] << endl;
    i += i & -i;
  }
}
 
ll rev_count(const vector<int> &a) {
  fill(bit.begin(), bit.end(), 0LL);
  const int offset = a.size()+1; // -Nまでありうるので，N+1かさあげ
  n = a.size() * 2 + 2;
  //  const int offset = a.size() + 1;
  ll ans = 0;
  int sum_v = 0; 
  add(sum_v, 1, offset); // 自分との比較のために追加
  REP(j,a.size()) {
    sum_v += a[j];
    ans += sum(sum_v, offset); // <= の条件のとき
    //cout << j << ":" << a[j] << ":"<< sum_v << ":" << ans << endl;
    add(sum_v, 1, offset);
  }
  return ans;
}

ll solve(const vector<ll> &data) {
  const int N = data.size();
  ll B = N; B *= (N+1); B/=2LL; B++; B/=2LL;
  //cout << "Border = " << B << endl;
  vector<int> cmp_v(N,0);
#ifdef DEBUG
  for(int i = 0; i <= 5; i++) {
    cout << "Go....." << i << endl;
    REP(j, N) {
      cmp_v[j] = data[j] >= i ? +1 : -1;
      cout << j << data[j] << cmp_v[j] << endl;
    }
    ll ret = rev_count(cmp_v);
    
    cout << i << " " << ret << endl;
  }
  return 0;
#endif
  ll low = 0, high = 1LL<<60;
  while(low + 1 < high) {
    ll mid = (low + high) / 2LL;
    REP(j, N) {
      cmp_v[j] = data[j] >= mid ? +1 : -1;
    }
    ll ret = rev_count(cmp_v);
    //cout << mid << " " << ret << endl;
    if(ret >= B) low = mid;
    else high = mid;
  }
  return low;
}

int main() {
  int N; cin>>N;
  vector<ll> a(N);
  vector<ll> bit(2*N+10, 0);
  REP(i,N) cin>>a[i];
  ll ans = a[0];
  if(N>1) ans = solve(a);
  cout << ans << endl;
  return 0;
}