#abc361c. C - Make Them Narrow

C - Make Them Narrow

Score : 250250 points

问题陈述

给定一个长度为 NN 的序列 AA。 从 AA 中自由选择恰好 KK 个元素并移除它们,然后将剩余的元素按照原始顺序连接起来,形成一个新的序列 BB。 找到这个的最小可能值:BB 的最大值减去 BB 的最小值。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a sequence AA of length NN.
Freely choose exactly KK elements from AA and remove them, then concatenate the remaining elements in their original order to form a new sequence BB.
Find the minimum possible value of this: the maximum value of BB minus the minimum value of BB.

Constraints

  • All inputs are integers.
  • 1K<N2×1051 \le K < N \le 2 \times 10^5
  • 1Ai1091 \le A_i \le 10^9

Input

The input is given from Standard Input in the following format:

NN KK

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer as an integer.

Sample Input 1

5 2
3 1 5 4 9

Sample Output 1

2

Consider removing exactly two elements from A=(3,1,5,4,9)A=(3,1,5,4,9).

  • For example, if you remove the 2nd element 11 and the 5th element 99, the resulting sequence is B=(3,5,4)B=(3,5,4).
    • In this case, the maximum value of BB is 55 and the minimum value is 33, so (maximum value of BB) - (minimum value of BB) =2=2, which is the minimum possible value.

Sample Input 2

6 5
1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

8 3
31 43 26 6 18 36 22 13

Sample Output 3

18