You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble nbarrels 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 nk 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 and1 ≤ 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堆数,每堆数的最小值相差不能大于limit ,

求出n堆数最小值的和

upper_bound 返回的是第一个大于的数,减去1就是小于等于的数了

 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
long long a[maxn];
int n, k, limit;
int main() {
scanf("%d%d%d", &n, &k, &limit);
for (int i = ; i < n * k ; i++)
scanf("%lld", &a[i]);
sort(a, a + n * k );
int temp = upper_bound(a, a + n * k, a[] + limit) - a;
long long ans = ;
int sum = n * k;
if (temp >= n) {
int temp1=temp;
while(sum > temp && sum - temp >= k - ) {
sum -= k - ;
ans += a[--temp1];
}
for (int i = ; i * k < temp1 ; i++)
ans += a[i * k];
}
printf("%lld\n", ans);
return ;
}

C. Liebig's Barrels的更多相关文章

  1. Codeforce Div-2 985 C. Liebig's Barrels

    http://codeforces.com/contest/985/problem/C C. Liebig's Barrels time limit per test 2 seconds memory ...

  2. codeforce 985C Liebig's Barrels(贪心+思维)

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

  3. CF985C Liebig's Barrels 贪心 第二十

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

  4. codeforces 985C Liebig's Barrels

    题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少,或者 ...

  5. Liebig's Barrels CodeForces - 985C (贪心)

    链接 大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和 假设最短板长$m$, 显然最后桶的体积都在$ ...

  6. codeforces 985C Liebig's Barrels(贪心)

    题目 题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少 ...

  7. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  8. 【codeforces 768F】 Barrels and boxes

    http://codeforces.com/problemset/problem/768/F (题目链接) 题意 A,B两种物品可以装到栈中,每个栈只能存放一种物品,容量没有限制.现在讲所有栈排成一列 ...

  9. 【codeforces 768F】Barrels and boxes

    [题目链接]:http://codeforces.com/problemset/problem/768/F [题意] 让你把f个food和w个wine装在若干个栈里面; 每个栈只能装food或者是wi ...

随机推荐

  1. 如果以一个树状的形式返回一个UIView的所有子视图

    该方法也是从一个视频中看到,总觉得会有很大作用,故记录在这里. 它返回一个xml的字符串,用火狐浏览器或者其他可以格式化xml的工具打开,即可查看其层级关系. /** * 返回传入view的所有层级结 ...

  2. 如何利用c中的指针实现两个8bit的数合并为16bit

    对于从事单片机开发,进行单片机c语言开发的人来说,在对外部信息采集回来的数据进行处理,经常会用到,将采集到的第一个字节作为高8位,采集到的第二个字节作为低8位,从而构成1个16bit的数,得到一次完整 ...

  3. python笔记--1

    pip工具常用命令: pip命令示例 说明 pip download SomePackage[==version] 下载扩展库的指定版本,不安装 pip freeze [> requiremen ...

  4. JAVA设计模式--学习总结(序)

    设计模式(Design pattern)是一套被反复使用的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 常见的设计模式有23种.分为三大类:创建型模式, ...

  5. VS2017安装包不占用C盘空间的方法,亲试

    问题:普通VS2017的安装方式,不论是在线安装还是下载的离线安装包,都会在安装过程中将安装包保存在C:\ProgramData\Microsoft\VisualStudio\Packages文件夹下 ...

  6. tar结果find打包指定后缀的文件

    find 目录名 -name "*.ini" | xargs tar czvf tarch.tar.gz  tar czf tmp.tar.gz tmp/ --exclude=&q ...

  7. 【计算机视觉】深度相机(一)--TOF总结

    http://www.voidcn.com/blog/lg1259156776/article/p-6302915.html 1.1 TOF初探 TOF是Time of flight的简写,直译为飞行 ...

  8. Using SSH and SFTP in Mac OS X

    http://answers.stat.ucla.edu/groups/answers/wiki/7a848/ SH and SFTP are command line applications av ...

  9. HDU-5705

    Clock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem De ...

  10. vfd折腾(一)

    从一开始驱动一块翻出来的液晶显示屏就想做一个电子时钟,偶然翻到了vfd(Vacuum Fluorescent Display的缩写,意为真空荧光显示屏). 此后就走上了不归路