#P11761. [IAMOI R1] 明码标价

[IAMOI R1] 明码标价

Description

There are nn items in the mall, where the ii-th item has price aia_i. Since Xiao C has decision fatigue, Xiao L wants to select an item through the following method:

Xiao L neither wants the cheapest item (poor quality) nor the most expensive one (low cost-effectiveness). Therefore, he defines the median of mm item prices as the price of the middle item after sorting prices in ascending order. Specifically, the median is the price of the m+12\lfloor\frac{m+1}{2}\rfloor-th item after sorting.

Xiao L plans to divide these nn items into continuous kk segments based on their utility, then take the median-priced item from each segment. Next, he will take the median of these selected items as the final purchase choice.

However, Xiao C disagrees with this plan because her partitioning differs from Xiao L's. They compromise by adopting the fairest approach: considering all possible partitioning schemes, collect all possible median prices (including duplicates from multiple schemes), then take the median of this collection as the final price.

Given the complexity of possible partitions, they need your help to determine the final selected price.

Formal Problem Statement

Define mid({a1,a2,,an})\operatorname{mid}(\{a_1,a_2,\cdots,a_n\}) as the median of multiset {a1,,an}\{a_1,\cdots,a_n\}, which equals the n+12\lfloor\frac{n+1}{2}\rfloor-th element after sorting in ascending order.

Given a sequence a1ana_1\sim a_n of length nn, define $f(l,r)=\operatorname{mid}(\{a_l,a_{l+1},\cdots,a_r\})$.

A partition is defined as:

  • A sequence ll of length kk (where 0kn0 \le k \le n) satisfying 1l1<l2<<lk<n1 \le l_1 < l_2 < \cdots < l_k < n when k0k \neq 0.
  • Two partitions are different if their kk values differ or any position in ll differs.

The weight of a partition is:

  • When k0k \neq 0: $\operatorname{mid}(\{f(1,l_1), f(l_1+1,l_2), \cdots, f(l_k+1,n)\})$
  • When k=0k = 0: mid({f(1,n)})\operatorname{mid}(\{f(1,n)\})

Find the median of the multiset containing weights of all distinct partitions.

Input Format

  • The first line contains a single integer nn.
  • The second line contains nn integers a1ana_1\sim a_n.

Output Format

Output a single integer representing the final selected price.

3
1 2 3
1

Hint

Sample Explanation

For n=3n=3, there are 4 partition schemes:

  1. {{1},{2},{3}}\{\{1\},\{2\},\{3\}\} → weight mid(1,2,3)=2\operatorname{mid}(1,2,3)=2
  2. {{1,2},{3}}\{\{1,2\},\{3\}\} → weight mid(1,3)=1\operatorname{mid}(1,3)=1
  3. {{1},{2,3}}\{\{1\},\{2,3\}\} → weight mid(1,2)=1\operatorname{mid}(1,2)=1
  4. {{1,2,3}}\{\{1,2,3\}\} → weight mid(2)=2\operatorname{mid}(2)=2

The final multiset is {1,1,2,2}\{1,1,2,2\} with median 11.

Constraints

Subtask scoring is used.

Subtask nn\le Special Property Score
1 15 None 13
2 40 A 17
3 None 20
4 100 A 23
5 None 27

Special Property A: aa is a permutation of 1n1\sim n.

For all data: 2n1002 \le n \le 100, 1ai1091 \le a_i \le 10^9.

Note: In C++, you may use __int128 to store integers in [2128,21281][-2^{128}, 2^{128}-1].

Translation by DeepSeek R1