PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000049. Trung vị của tất cả trung vị đoạn con (Median of Medians)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trung vị của tất cả trung vị đoạn con (Median of Medians)

Median of Medians

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

For each nonempty contiguous subarray, compute its median. Collect the medians of all subarrays, and find the median of that collection. For a sequence of length mm, the median is its ⌊m/2⌋+1\lfloor m/2\rfloor+1-th smallest element (1-based): for even length, use the right middle.

Input

The first line is nn. The second line contains nn integers aia_i.

Output

Print the final median.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤ai≤1091\le a_i\le10^9.
  • Subtask 2 (30 points): 1≤n≤1501\le n\le 150; 1≤ai≤1091\le a_i\le10^9.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤ai≤1091\le a_i\le10^9.

Examples

Input

3
10 30 20

Output

30

Explanation

The six medians are 10,30,20,30,30,20. Sorting yields 10,20,20,30,30,30; the right-middle (fourth) element is 30.