The Jury's Best Median

medium binary-search prefix-sums arrays 1000 ms · 256 MB

The Jury's Best Median

At the regional baking contest, n judges sit side by side along one long table, numbered 1 to n from left to right. Judge i has already written down a score a_i for the contestant's cake.

The organiser, who is quite fond of this particular cake, is allowed to choose which judges form the official panel, subject to two rules:

  • the panel must be a group of judges sitting next to each other, that is, judges l, l+1, …, r for some 1 ≤ l ≤ r ≤ n;
  • the panel must contain at least k judges, that is, r − l + 1 ≥ k.

The panel's verdict is computed like this: write down the scores of its m = r − l + 1 judges, sort them in non-decreasing order, and take the score in position ⌈m/2⌉ (positions counted from 1). So a panel of 5 judges takes its 3rd smallest score, and a panel of 4 judges takes its 2nd smallest score: for an even-sized panel it is the lower of the two middle scores. Equal scores are kept as separate entries when sorting.

Find the largest verdict the organiser can achieve.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2·10^5).

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), the judges' scores from left to right.

Output

Print one integer: the largest possible verdict over all allowed panels.

Notes

In the first example the panels of exactly three judges have verdicts 4 (scores 4 1 5), 2 (1 5 2) and 3 (5 2 3). Larger panels do no better: judges 1–4 give sorted scores 1 2 4 5 and verdict 2, and all five judges give verdict 3. The answer is 4.

In the second example every panel of two judges contains a low score, and because an even panel takes the lower middle score, each of them has a verdict of at most 3. The panel of judges 1–3 has sorted scores 2 7 8 and verdict 7, which is the best possible.

Example 1

Input

5 3
4 1 5 2 3

Output

4

Example 2

Input

5 2
7 2 8 3 6

Output

7

Hints · reveal one at a time if you're stuck

Hint 1

The question is about the largest value that can be the verdict of some valid panel. Ask whether a verdict of at least some threshold x is achievable, and whether that answer can only change in one direction as x changes.

Hint 2

For a fixed threshold x, a panel's verdict is at least x exactly when few of its scores are below x. Mark each score as +1 when it is at least x and -1 otherwise. Work out which sign of the panel's sum matches the lower median condition, paying attention to even-sized panels.

Hint 3

With prefix sums of the marks, a valid panel is a pair of prefix positions at least k apart whose difference has the right sign. Sweeping the right end and tracking the smallest prefix among allowed left ends checks a threshold in linear time. Combine this with a binary search over the threshold.

report an issue Prefer your own editor? sooks prep 43 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.