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

#BS0000354. Dãy con chẵn lẻ (Odd-Even Subsequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy con chẵn lẻ (Odd-Even Subsequence)

Odd-Even Subsequence

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Ashish has an integer sequence aa of length nn. A subsequence is formed by deleting zero or more entries while retaining the order of the remaining entries.

Consider a subsequence ss of length exactly kk, numbered from 11 through kk. Let M1M_1 and M2M_2 be the largest entries at odd and even positions of ss, respectively. Its cost is min⁡(M1,M2)\min(M_1,M_2). Both groups are nonempty because k≥2k\ge2.

Determine the minimum cost among all such subsequences. Parity refers to positions in ss, not the original array.

Input

The first line contains integers n,kn,k, the original length and required subsequence length.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

Print one integer: the minimum cost.

Subtasks

  • Subtask 1 (20 points): 2≤k≤n≤142\le k\le n\le 14; 1≤ai≤10001\le a_i\le 1000 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 2≤k≤n≤20002\le k\le n\le 2000; 1≤ai≤10001\le a_i\le 1000 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 2≤k≤n≤2000002\le k\le n\le 200000; 1≤ai≤10000000001\le a_i\le 1000000000 for every 1≤i≤n1\le i\le n.

Examples

Input 1

4 2
1 2 3 4

Output 1

1

Explanation

Choose s=(1,3)s=(1,3), whose odd and even maxima are 11 and 33. Its cost is 11, a lower bound because all entries are at least 11.

Input 2

4 3
1 2 3 4

Output 2

2

Explanation

Choose s=(1,2,4)s=(1,2,4), with maxima 44 and 22. A cost of 11 would require two entries at most 11 in odd positions or a 11 in the middle. The sole 11 is first in the original array, ruling out both.

Input 3

5 3
5 3 4 2 6

Output 3

2

Explanation

Choose s=(4,2,6)s=(4,2,6); the maxima are 66 and 22. No smaller cost is possible because all entries are at least 22.

Input 4

6 4
5 3 50 2 4 5

Output 4

3

Explanation

Choose s=(3,50,2,4)s=(3,50,2,4), giving maxima 33 and 5050. A cost below 33 requires two entries at most 22 in one parity group, but only one exists.