-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path985C. Liebig's Barrels.cpp
51 lines (42 loc) · 977 Bytes
/
985C. Liebig's Barrels.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Idea:
- Greedy.
- Sort the given array and try to find the maximum number of elements
such that the subtraction value between the maximum and the minimum
is less than or equal to `l`.
- From the set of elements in the previous step we need to choose `n`
of them to use them as the minimum staves in each barrel.
*/
#include <bits/stdc++.h>
using namespace std;
int n, k, l, mn = 2e9, have, a[100001];
int main() {
cin >> n >> k >> l;
for(int i = 0; i < n * k; ++i) {
cin >> a[i];
mn = min(mn, a[i]);
}
sort(a, a + n * k);
for(int i = 0; i < n * k; ++i) {
if(a[i] - mn > l)
break;
have = i + 1;
}
if(have < n) {
puts("0");
return 0;
}
int rem = n * k - have, take = 0;
long long tot = 0;
for(int i = have - 1; take < n && i >= 0; --i) {
if(rem < k - 1) {
++rem;
continue;
}
rem -= k - 1;
tot += a[i];
++take;
}
cout << tot << endl;
return 0;
}