//类似指针的想法

//偏移

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.

#define happy#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define P pair<int,int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}const int N=1e5+10;
int need[N],a[N];
int main(){
#ifdef happyfreopen("in.txt","r",stdin);
#endifint n=rd(),k=rd(),l=rd();int m=n*k;rep(i,0,m-1)a[i]=rd();sort(a,a+m);int num=0;for(int i=0;i<m;i++){if(a[i]-a[0]<=l)num++;}if(num<n)return puts("0"),0;rep(i,0,n-1){need[i]++;num--;}rep(i,0,n-1){int z=min(k-1,num);need[i]+=z;num-=z;}ll t=0,ans=0;rep(i,0,n-1){ans+=a[t];t+=need[i];}printf("%lld\n",ans);
}

codeforces+contest985C. Liebig's Barrels+greedy相关推荐

  1. 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 ...

  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-985C Liebig's Barrels

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

  4. C. Liebig's Barrels

    You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble nbarrels consisti ...

  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. codeforces 解题报告 978B. File Name strings greedy

    http://codeforces.com/contest/978/problem/B 解题思路: 1.不能出现"xxx"这样的串,贪心删掉多余的x就好了 2.遍历一遍字符串,遇到 ...

最新文章

  1. 岭南六少个人独立博客开通啦
  2. Java中PreparedStatement和Statement的用法区别
  3. Java时间对比------实际时间和设置时间对比
  4. php麻将机器人ai算法,高性能麻将AI算法
  5. 第二章 Java基本语法(一)
  6. Shell脚本里调用Python程序
  7. ModelArts微认证零售客户分群知识点总结
  8. 【Flink】FlinkConsumer是如何保证一个partition对应一个thread的
  9. js中数组的一些操作
  10. mysql enclosed by_MySQL导入格式化数据
  11. java certification_Java Certification.Status方法代码示例
  12. sysbench压测cpu,io,memory,threads,mutex
  13. 机器人正运动学(7)—— 连杆坐标系与DH参数
  14. Revit二次开发——一个简单的插件
  15. 面向金融的R语言——Lecture9
  16. python经纬度转换为平面坐标的算法_Python经纬度坐标转换为距离及角度的实现
  17. 妙计叠出,谋而后动——融云以“锦囊”之策,守护客户全生命周期
  18. Python模块之Shapely
  19. 日语输入法(OpenWnn)的代码学习以及修改记录
  20. 7-34 任务调度的合理性(25 分)

热门文章

  1. 联想及华硕笔记本重装Windows10系统过程及难点记录
  2. linux 异星工厂服务器,Factorio服务器部署指南
  3. Dplayer使用注意点
  4. %3c %3e 转换html,使用JavaScript将HTML转换为data:text / html链接
  5. NOIP 2018 真・退役记
  6. 想要语音转文字,有这个方法就够了
  7. 《ReactNative》app调用小程序的分享
  8. 性能大PK count(*)、count(1)和count(列)
  9. 迷信一把:一螺巧,二螺富……
  10. 美容预约平台+后台管理系统|前后分离VUE