Problem B

Statement
Copy Copied
Description:
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.

You need to permute the array elements so that value

$$\sum_{i=1}^{n-k}|A[i]-A[i+k]|$$

Input Format:
The first line contains two integers n, k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ min(5000, n - 1)).

The second line contains n integers A[1], A[2], ..., A[n] ( - 109 ≤ A[i] ≤ 109), separate by spaces — elements of the array A.

Output Format:
Print the minimum possible value of the sum described in the statement.

Note:
In the first test one of the optimal permutations is 1 4 2.

In the second test the initial order is optimal.

In the third test one of the optimal permutations is 2 3 4 4 3 5.

Evaluations

Eval IDRun IDProviderModelLangSuccessTimestampPromptResponseStdoutStderrRetry
105 20260126-054408 vertex gemini-3-pro-preview go false 2026-01-26T06:30:04.629701Z View View View View Retry