/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君が主催するプログラミングコンテストでは、 N 人の参加者を 1 つ以上のチームに分けて団体戦を行います。参加者には 1 から N までの番号が付けられており、参加者 i のレートは A_i です。
高橋君は、以下の条件をすべて満たすように参加者をチームに分割することにしました。
- すべての参加者はちょうど 1 つのチームに所属する。
- 各チームには K 人以上の参加者が所属する。
チーム内の実力のばらつきを小さくするため、あるチームの「不均衡度」を、そのチームに所属する参加者のレートの最大値と最小値の差として定義します。
すべてのチームの不均衡度の合計として達成可能な最小値を求めてください。
ただし、条件を満たす分割が存在しない場合は、 -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で与えられる。
N K A_1 A_2 \dots A_N
- 1 行目には、参加者の人数を表す整数 N と、各チームの人数の下限を表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各参加者のレートを表す N 個の整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
出力
すべてのチームの不均衡度の合計として達成可能な最小値を 1 行で出力せよ。
条件を満たす分割が存在しない場合は、 -1 を出力せよ。
入力例 1
5 2 10 1 4 15 3
出力例 1
8
入力例 2
3 5 100 200 150
出力例 2
-1
入力例 3
12 3 25 100 22 102 105 30 200 205 201 28 203 101
出力例 3
18
入力例 4
30 4 48 12 99 35 14 88 52 91 19 33 85 41 18 95 38 72 50 22 67 93 45 78 61 29 83 55 69 75 63 31
出力例 4
64
入力例 5
1 1 1000000000
出力例 5
0
Score : 400 pts
Problem Statement
In a programming contest hosted by Takahashi, N participants will be divided into one or more teams to compete in a team competition. The participants are numbered 1 to N, and the rating of participant i is A_i.
Takahashi has decided to divide the participants into teams such that all of the following conditions are satisfied:
- Every participant belongs to exactly one team.
- Each team has at least K participants.
To reduce the disparity in skill within teams, the "imbalance" of a team is defined as the difference between the maximum and minimum ratings among the participants belonging to that team.
Find the minimum possible sum of the imbalances of all teams.
If no division satisfies the conditions, print -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq K \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given in the following format:
N K A_1 A_2 \dots A_N
- The first line contains an integer N representing the number of participants and an integer K representing the lower bound on the number of participants in each team, separated by a space.
- The second line contains N space-separated integers A_1, A_2, \dots, A_N representing the ratings of the participants.
Output
Print the minimum possible sum of the imbalances of all teams in a single line.
If no division satisfies the conditions, print -1.
Sample Input 1
5 2 10 1 4 15 3
Sample Output 1
8
Sample Input 2
3 5 100 200 150
Sample Output 2
-1
Sample Input 3
12 3 25 100 22 102 105 30 200 205 201 28 203 101
Sample Output 3
18
Sample Input 4
30 4 48 12 99 35 14 88 52 91 19 33 85 41 18 95 38 72 50 22 67 93 45 78 61 29 83 55 69 75 63 31
Sample Output 4
64
Sample Input 5
1 1 1000000000
Sample Output 5
0