-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
38 lines (35 loc) · 1022 Bytes
/
Copy pathB.cpp
File metadata and controls
38 lines (35 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define long long long
#define err(a...) fprintf(stderr, a)
constexpr int N = 3e5 + 10;
constexpr int K = 5010;
constexpr int INF = 2e9;
int a[N], dp[K][K];
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
sort(a, a + n);
fill(&dp[0][0], &dp[K - 1][K - 1] + 1, INF);
dp[0][0] = 0;
int each = n / k, big = n % k;
for (int large = 0; large <= big; ++large) {
for (int small = 0; small <= k - big; ++small) {
int nx = large * (each + 1) + small * each;
if (large < big) {
int added = a[nx + each] - a[nx];
dp[large + 1][small] = min(dp[large + 1][small], dp[large][small] + added);
}
if (small < k - big) {
int added = a[nx + each - 1] - a[nx];
dp[large][small + 1] = min(dp[large][small + 1], dp[large][small] + added);
}
}
}
printf("%d", dp[big][k - big]);
return 0;
}