CodeForces 580B Kefa and Company(请客工资差值问题)

B. Kefa and Company

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Kefa wants tocelebrate his first big salary by going to restaurant. However, he needscompany.

Kefa has nfriends, each friend will agree to go to the restaurant if Kefa asks. Eachfriend is characterized by the amount of money he has and the friendship factorin respect to Kefa. The parrot doesn't want any friend to feel poor compared tosomebody else in the company (Kefa doesn't count). A friend feels poor if inthe company there is someone who has at leastdunits of money more than he does. Also, Kefa wants the total friendship factorof the members of the company to be maximum. Help him invite an optimalcompany!

Input

The first line of theinput contains two space-separated integers,nandd(1 ≤ n ≤ 105,) — the number of Kefa's friends and theminimum difference between the amount of money in order to feel poor,respectively.

Next nlines contain the descriptions of Kefa's friends, the(i + 1)-thline contains the description of thei-thfriend of typemi,si(0 ≤ mi, si ≤ 109)— the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendshipfactir that can be reached.

Examples

Input

4 5
75 5
0 100
150 20
75 1

Output

100

Input

5 100
0 7
11 32
99 10
46 8
87 54

Output

111

Note

In the first sampletest the most profitable strategy is to form a company from only the secondfriend. At all other variants the total degree of friendship will be worse.

In the second sample test we can takeall the friends.

 

 

参考题意:

一个人想请客,但是请的客人的收入差值不能够超出给定的数字。最后输出的是最大值。


参考思路:

按照工资升序排序,首先是最低工资,遍历一遍数据,当差值超过给定值,更新最低工资。找出该范围的人数总和。

 

参考代码:

 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define MYDD 113000

using namespace std;

struct Q {
	int money;//该类朋友的工资
	int friends;//此类朋友人数
} dd[MYDD];

bool cmp_money(Q x,Q y) {
	return x.money<y.money;//按照工资升序排序
}

bool cmp_ANS(__int64 x,__int64 y) {
	return x>y;
}

__int64 MAX(__int64 x,__int64 y) {
	return x>y? x:y;
}

int main() {
	int n,d,min;
	__int64 ANS[MYDD],ans;
	while(scanf("%d%d",&n,&d)!=EOF) {
		memset(ANS,0,sizeof(ANS));
		ans=0;
		for(int j=0; j<n; j++)
			scanf("%d%d",&dd[j].money,&dd[j].friends);

		sort(dd,dd+n,cmp_money);

		/*int k=0;
		min=dd[0].money;//取第一类朋友的工资
		for(int j=0; j<n; j++) {
			if(dd[j].money-min>=d) {//二者的差值小于给定值 d 
				min=dd[j].money;//寻找下一类
				k++;
				ANS[k]=dd[j].friends;
			} else {
				ANS[k]+=dd[j].friends;
			}
			ans=MAX(ans,ANS[k]);//计算出最多的朋友类
		}*/

		__int64 ans=0,cur=0,st=0;
		for(int i=0; i<n;) {
			if(dd[i].money-dd[st].money>=d) {
				cur-=dd[st].friends;
				++st;
			} else {
				cur+=dd[i].friends;
				++i;
			}
			ans=max(ans,cur);
		}

		printf("%I64d\n",ans);
	}
	return 0;
}

/*

5 100
0 7
11 32
99 10
46 8
87 54

6 100
0 7
11 32
99 10
46 8
87 54
187 1

10 100
0 7
11 32
99 10
46 8
87 54
188 110
281 200
300 9
999 989
897 990

1 999
123 78784
111
111
310

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值