Educational Codeforces Round 44 (Rated for Div. 2) C - Liebig's Barrels

C. Liebig's Barrels
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.

Let volume vj of barrel j be equal to the length of the minimal stave in it.

You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.

Input

The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109).

The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.

Output

Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Examples
Input
Copy
4 2 1
2 2 1 2 3 2 2 3
Output
Copy
7
Input
Copy
2 1 0
10 10
Output
Copy
20
Input
Copy
1 2 1
5 2
Output
Copy
2
Input
Copy
3 2 1
1 2 3 4 5 6
Output
Copy
0

Note

In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].

In the second example you can form the following barrels: [10], [10].

In the third example you can form the following barrels: [2, 5].

In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.


题意:给你n*k个木板,每个木桶由k个木板组成,容量由最短的那块木板决定,并且要满足任意两个木桶之间的容量差不能大于l,否则的话输出0,反之输出n个木桶的最大容量之和。
分析:
贪心+模拟
mp1是一个栈,mp2是一个队列,ans记录每个木桶的容量之和。
先把n*k块木板按升序排列,把长度大于a[1]+l的放入mp2中,长度小于等于a[1]+l的木板放入mp1中,然后从mp1中依次取出一个木板,用mp2中的k-1个木板来凑一个木桶,当发现mp2中的木板不足以来凑时,那就用mp1中的木板来凑。最后输出ans就行了。
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
ll a[1000000];
int main()
{
    ll n,k,l;
    while(~scanf("%lld%lld%lld",&n,&k,&l))
    {
        stack mp1;    ///记录比a[1]+l小的数
        queue mp2;    ///记录比a[1]+l大的数
        for(int i=1; i<=n*k; i++)
            scanf("%lld",&a[i]);
        sort(a+1,a+1+n*k);
        ll maxn=a[1]+l;
        for(int i=1; i<=n*k; i++)
        {
            if(a[i]>maxn)
                mp2.push(a[i]);
            else
                mp1.push(a[i]);
        }
        ll ans=0;
        int cnt=0;
        if(mp1.size()

你可能感兴趣的:(codeforces)