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

#BS0000353. Giảm nhỏ nhất chênh lệch lớn nhất của các cặp (Minimize the Maximum Difference of Pairs)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giảm nhỏ nhất chênh lệch lớn nhất của các cặp (Minimize the Maximum Difference of Pairs)

Minimize the Maximum Difference of Pairs

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An array aa contains nn nonnegative integers indexed from 00 to n−1n-1. Select exactly pp pairs of indices (i,j)(i,j), where 0≤i<j<n0\le i<j<n, using each index at most once.

A pair has difference ∣ai−aj∣|a_i-a_j|. The cost of a selection is the largest difference among its pairs, or 00 when p=0p=0. Determine the minimum achievable cost.

Input

The first line contains integers n,pn,p, the array length and required number of pairs.

The second line contains nn integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}.

Output

Print the minimum cost as one integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤141\le n\le 14; 0≤p≤⌊n/2⌋0\le p\le\lfloor n/2\rfloor; 0≤ai≤10000\le a_i\le 1000 for every 0≤i<n0\le i<n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 0≤p≤⌊n/2⌋0\le p\le\lfloor n/2\rfloor; 0≤ai≤10000\le a_i\le 1000 for every 0≤i<n0\le i<n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 0≤p≤⌊n/2⌋0\le p\le\lfloor n/2\rfloor; 0≤ai≤10000000000\le a_i\le 1000000000 for every 0≤i<n0\le i<n.

Examples

Input 1

6 2
10 1 2 7 1 3

Output 1

1

Explanation

Choose indices (1,4)(1,4) with values 1,11,1 and (2,5)(2,5) with values 2,32,3. The differences are 00 and 11. Only one equal-value pair exists, so two zero-difference pairs are impossible.

Input 2

4 1
4 2 1 2

Output 2

0

Explanation

Indices 1,31,3 both hold 22. Their difference is 00, the lower bound for any difference.

Input 3

1 0
7

Output 3

0

Explanation

No pairs are required since p=0p=0; the defined cost is 00.