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 12 2 1 2 3 2 2 3

output

Copy

7

input

Copy

2 1 010 10

output

Copy

20

input

Copy

1 2 15 2

output

Copy

2

input

Copy

3 2 11 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就是小于等于的数了

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 10;
 4 long long a[maxn];
 5 int n, k, limit;
 6 int main() {
 7     scanf("%d%d%d", &n, &k, &limit);
 8     for (int i = 0 ; i < n * k ; i++)
 9         scanf("%lld", &a[i]);
10     sort(a, a + n * k );
11     int temp = upper_bound(a, a + n * k, a[0] + limit) - a;
12     long long ans = 0;
13     int sum = n * k;
14     if (temp >= n) {
15         int temp1=temp;
16         while(sum > temp && sum - temp >= k - 1) {
17             sum -= k - 1;
18             ans += a[--temp1];
19         }
20         for (int i = 0 ; i * k < temp1 ; i++)
21             ans += a[i * k];
22     }
23     printf("%lld\n", ans);
24     return 0;
25 }

转载于:https://www.cnblogs.com/qldabiaoge/p/9071432.html

C. Liebig's Barrels相关推荐

  1. CodeForces-985C Liebig's Barrels

    CodeForces-985C Liebig's Barrels Description You have m = n·k wooden staves. The i-th stave has leng ...

  2. cf Educational Codeforces Round 44 C. Liebig's Barrels

    原题: C. Liebig's Barrels time limit per test2 seconds memory limit per test256 megabytes inputstandar ...

  3. codeforces+contest985C. Liebig's Barrels+greedy

    //类似指针的想法 //偏移 C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. Codeforces #985C Liebig's Barrels

    [问题描述] You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels ...

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

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

  6. Codeforces 985C (贪心)

    传送门 题面: C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. coderforce Educational Codeforces Round 44 (Rated for Div. 2) C(赛后补题)

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

  8. 【codeforces 768F】 Barrels and boxes

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

  9. python电影数据分析的代码_python-small-examples

    Kaggle电影数据分析实战 本项目基于Kaggle电影影评数据集,通过这个系列,你将学到如何进行数据探索性分析(EDA),学会使用数据分析利器pandas,会用绘图包pyecharts,以及EDA时 ...

最新文章

  1. TensorFlow被曝存在严重bug,搭配Keras可能丢失权重,用户反映一个月仍未修复
  2. GNU Readline 库及编程简介【转】
  3. 12.04 深圳站 | Serverless Developer Meetup 开放报名
  4. 登录mysql报错2059_navicat连接mysql8.0+版本报错2059
  5. 深度学习简介(一)——卷积神经网络
  6. haproxy配置代理tomcat和nginx_你真的掌握LVS、Nginx及HAProxy工作原理吗?
  7. 简述linux中动态库和静态库的制作调用流程
  8. js和jsp所有页面跳转总结
  9. latex导数_Latex:导数【高中常用公式】
  10. Linux——VMware虚拟机安装CentOS步骤
  11. android sha1和签名证书的学习
  12. Python爬虫之selenium高级功能
  13. SharePoint 2013 托管导航及相关配置
  14. vs 自动生成core dump文件
  15. Phoenix报错(5) org.apache.hadoop.hive.ql.metadata.HiveException
  16. 计算机cmd复制粘贴指令,win7系统使用CMD命令复制和删除文件夹的方法
  17. ios开发swift_10位Swift和iOS开发大师
  18. 【rviz_plugin Goal3DTools 深蓝路径规划 PluginlibFactory】
  19. VOB视频格式转换器怎么把vob转换为mp3
  20. 无法使用内置管理员账户打开照片,请使用其他账户登录,然后再试一次

热门文章

  1. JAVA应用开发MQ实战最佳实践——Series2:消息队列RocketMQ性能测试案例
  2. AI 云原生浅谈:好未来 AI 中台实践
  3. 智能工作:人工智能将如何重塑远程工作
  4. 5G与WiFi 6之争,对物联网意味着什么
  5. 如何基于Weex实现创新交互体验?手淘开源技术BindingX案例解析
  6. 三招做出页面中的节奏与韵律
  7. 以《剑与远征》为例,浅析新型挂机放置游戏的游戏特色特点
  8. RPG+模拟经营,《喧嚣的城堡》如何做闭环设计?
  9. Oracle---对字段进行修改(varchar2修改clob)+ 索引失效
  10. Oracle之外部表