Maximum Sum of Chosen Subset's Adjacent Differences

EASY · C · 2000ms

For a smaller exhibition team of exactly K players (not paired, just a group), Zayan wants to maximize the sum of skill-gaps between every two players in that group. Given N sorted-after-you-sort integers, choose any subset of size K to MAXIMIZE the sum of absolute differences between all C(K,2) pairs in the subset. Print the maximum sum for a given K.

Input

Line1: N K. Line2: N integers.

Output

Maximum sum.

Constraints

1 ≤ K ≤ N ≤ 10^5

Sample Input

5 3
1 2 3 4 5

Sample Output

8
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Maximum Sum of Chosen Subset's Adjacent Differences · DIU ContestHub