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. SharePoint 添加BCD菜单

    前言:在SharePoint中,我们常见的操作就是添加我们的自定义BCD菜单,下面,简单介绍下添加自定义BCD菜单的操作.主要介绍两种熟悉的方法,一种通过xml方式,另一种是通过js的方式. 环境:S ...

  2. IOS常见的加密方法,常用的MD5和Base64

    iOS代码加密常用加密方式 iOS代码加密常用加密方式,常见的iOS代码加密常用加密方式算法包括MD5加密.AES加密.BASE64加密,三大算法iOS代码加密是如何进行加密的,且看下文 MD5 iO ...

  3. [Zabbix3.0 ]添加Nginx监控

    通过Nginx的http_stub_status_module模块提供的状态信息来监控,所以在Agent端需要配置Nginx状态获取的脚本,和添加key信息等,然后在Server端配置Nginx的监控 ...

  4. SQLSERVER 性能优化之Perfmon指标

    Perfmon是Windows系统性能监视程序.用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Processor/%Privileged Time阀值:如果数值持续大于75%就表示存在 ...

  5. LambdaToSql 发布 兰姆达转换sql

    文档目录索引 查询.函数.分组.排序.分页 添加 Insert into 编辑 Update set 删除 Delete 生成实体 内置常用工具类库  文档完善中... 事务处理 Join 连接查询 ...

  6. jquery选择器项目实例分析

    首先废话一句,jQuery选择器真心很强大!  在项目中遇到这么一个问题easyui的问题 如图所示,当前页面显示的是"原始报文查询"的页面,当时左侧导航栏却选中的是"重 ...

  7. java并发包小结(一)

    java.util.concurrent 包含许多线程安全.高性能的并发构建块.换句话讲,创建 java.util.concurrent 的目的就是要实现 Collection 框架对数据结构所执行的 ...

  8. tomcat jvm优化

    tomcat优化(全) (2012-09-26 10:12:59) 转载▼ 标签: 杂谈 分类: java 1.内存设置(VM参数调优)(1). Windows环境下,是tomcat解压版(执行sta ...

  9. Mac下通过brew安装指定版本的nodejs

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "PingFang SC Semibold"; color: #2c303 ...

  10. Ocelot中文文档-路由

    Ocelot的主要功能是接管进入的http请求并把它们转发给下游服务.目前是以另一个http请求的形式(将来可能是任何传输机制). Ocelot将路由一个请求到另一个请求描述为ReRoute.为了在O ...