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

#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

def cumulative_sum(arr)
  arr.reduce([0]) do |prev, cur|
    prev.push(prev.last + cur)
    prev
  end
end

# 添字は1から
class Bit
  UNITY_SUM = 0

  attr_accessor :dat

  # [1, n)
  def initialize(n)
    @dat = []
    (n+1).times do |i|
      dat[i] = UNITY_SUM
    end
  end

  # a is 1-indexed
  # a番目の要素にxを足す
  def add(a, x)
    i = a
    while i < dat.length
      dat[i] += x
      i += i & -i
    end
  end

  # [1, a], a is 1-indexed
  def sum(a)
    res = UNITY_SUM
    i = a
    while i > 0
      res += dat[i]
      i -= i & -i
    end
    res
  end

  # [a, b), a and b are 1-indexed
  # bは含まれない
  def sum_section(a, b)
    sum(b-1) - sum(a-1)
  end

  # k番目の数を取得する
  # 各値の数を数え上げる場合のみ有効なメソッド
  # add(x, 1) と add(x, -1) しかしないとき
  def get_kth_when_counting(k)
    k -= 1
    high = dat.length
    low = 0

    while high - low > 1
      mid = low + (high - low) / 2

      if sum(mid) > k
        high = mid
        elsei
        low = mid
      end
    end
    high
  end
end

N = get_int
A = get_ints

max = A.max

high = max
low = 1

threshold = (N * (N+1))/4 # 中央地の個数(1-Nの和)の半分

while high > low + 1
  mid = low + (high - low) / 2

  ax = A.map { |a| a > mid ? 1 : -1 }
  ruiseki = cumulative_sum(ax)

  # 転倒数をBITで求めるため、0以下のものは加算して1以上にする（加算しても転倒数は変わらない）
  min = ruiseki.min
  if min <= 0
    ruiseki = ruiseki.map { |x| x - (min - 1) }
  end
  ruiseki_max = ruiseki.max
  bit = Bit.new(ruiseki_max)
  tentou_su = 0
  ruiseki.each.with_index do |r, i|
    one_index = i + 1
    bit.add(r, 1)
    tentou_su += one_index - bit.sum(r)
  end

  # p "mid: #{mid}, tentou: #{tentou_su}"
  if tentou_su > threshold
    high = mid
  else
    low = mid
  end
end

puts high