codeforces 580B Kefa and Company

来源:互联网 发布:手机相机软件排行 编辑:程序博客网 时间:2024/04/29 05:00

B. Kefa and Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.

Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!

Input

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

Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type misi(0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendship factir that can be reached.

Examples
input
4 575 50 100150 2075 1
output
100
input
5 1000 711 3299 1046 887 54
output
111
Note

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

In the second sample test we can take all the friends.


题目大意:每个朋友有自己的m和s,要求每个人之间的m值不超过d值,求最大的s总和

扫一遍就可以了

#include<iostream>#include<algorithm>#include<cstdio>using namespace std;typedef long long ll;const int maxn=1e5+7;struct Node{int m;int s;}fri[maxn];bool cmp(Node a,Node b){return a.m<b.m;}int main(){int n,d;while(cin>>n>>d){for(int i=0;i<n;i++){scanf("%d%d",&fri[i].m,&fri[i].s);}sort(fri,fri+n,cmp);int l=0,r=0;ll maxx=0;ll ans=0;while(r!=n){if(fri[r].m-fri[l].m<d){ans+=fri[r].s;maxx=max(ans,maxx);r++;}else{ans-=fri[l].s;l++;}}cout<<maxx<<endl;}}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 面试防汛值班发生灾情你怎么办 怀孕上班路途太远怎么办 硕士错过校招应该怎么办 收银员收多了钱怎么办 商铺贷款批不了怎么办 铁路局的门面乱收房租怎么办 酒店夜审房费多过怎么办 夜审房价录多了怎么办 做工地拿不到钱怎么办 赢了官司拿不到钱怎么办 工地上拿不到钱怎么办 做了工拿不到钱怎么办 高速公路上车没油了怎么办 高铁乘务员年龄大了怎么办 总公司跑路了分公司怎么办 坐车久了耳朵懵怎么办 过完隧道耳朵疼怎么办 护照还在大使馆需要出国怎么办 护照在大使馆不返回怎么办 美国面签迟到了怎么办 成都美签迟到了怎么办 签证电调没人接怎么办 单位没有抬头纸怎么办在职证明 出国签证无银行流水怎么办 铁路职工得癌症后工作怎么办 去泰国不会泰语和英语怎么办 签证状态一直没有更新怎么办 简理财不能身份信息确认怎么办 德国领事馆没有收到预约邮件怎么办 父母一方带孩子英国签证怎么办 去韩国自由行签证怎么办 韩国自由行签证的该怎么办 法院离婚判决书没了怎么办 离婚判决书对方没收到怎么办 法院判离怎么办离婚证 判决书下来后没钱还怎么办 拿调解书怎么办离婚证 判决书生效后对方拒不履行怎么办 收到民事判决公告该怎么办 苹果手机gps信号弱怎么办 二审败诉后拿到判决书怎么办