atcoder#ARC101B. [ABC107D] Median of Medians

[ABC107D] Median of Medians

Score : 700700 points

Problem Statement

We will define the median of a sequence bb of length MM, as follows:

  • Let bb' be the sequence obtained by sorting bb in non-decreasing order. Then, the value of the (M/2+1)(M / 2 + 1)-th element of bb' is the median of bb. Here, // is integer division, rounding down.

For example, the median of (10,30,20)(10, 30, 20) is 2020; the median of (10,30,20,40)(10, 30, 20, 40) is 3030; the median of (10,10,10,20,30)(10, 10, 10, 20, 30) is 1010.

Snuke comes up with the following problem.

You are given a sequence aa of length NN. For each pair (l,r)(l, r) (1lrN1 \leq l \leq r \leq N), let ml,rm_{l, r} be the median of the contiguous subsequence (al,al+1,...,ar)(a_l, a_{l + 1}, ..., a_r) of aa. We will list ml,rm_{l, r} for all pairs (l,r)(l, r) to create a new sequence mm. Find the median of mm.

Constraints

  • 1N1051 \leq N \leq 10^5
  • aia_i is an integer.
  • 1ai1091 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

NN

a1a_1 a2a_2 ...... aNa_N

Output

Print the median of mm.

3
10 30 20
30

The median of each contiguous subsequence of aa is as follows:

  • The median of (10)(10) is 1010.
  • The median of (30)(30) is 3030.
  • The median of (20)(20) is 2020.
  • The median of (10,30)(10, 30) is 3030.
  • The median of (30,20)(30, 20) is 3030.
  • The median of (10,30,20)(10, 30, 20) is 2020.

Thus, m=(10,30,20,30,30,20)m = (10, 30, 20, 30, 30, 20) and the median of mm is 3030.

1
10
10
10
5 9 5 9 8 9 3 5 4 3
8