[关闭]
@xunuo 2017-02-22T16:49:14.000000Z 字数 2494 阅读 960

Codeforces 580 B. Kefa and Company


time limit per test 2 seconds memory limit per test256 megabytes

结构体 队列


Description

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 mi, si (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 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 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.

题意:

有一个人,他举行了一场宴会,邀请了n个朋友来参加,来的朋友带有两个值:友谊值和金钱,如果他们之间的友谊值之差>=d,那么他们其中就会有一个不来,问你这个人最后收到的money最多是多少?

解题思路:

这儿要利用到结构体排序,然后找他们之间差值<d的人,求能够得到的的money的最大值

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. #define N 100010
  8. #define ll long long
  9. struct node
  10. {
  11. ll m,s;
  12. }a[N],f;
  13. bool cmp(node x,node y)
  14. {
  15. if(x.m==y.m)
  16. return x.s<y.s;
  17. return x.m<y.m;
  18. }
  19. int main()
  20. {
  21. ll n,d;
  22. while(scanf("%lld%lld",&n,&d)!=EOF)
  23. {
  24. queue<node>q;
  25. memset(a,0,sizeof(a));
  26. for(int i=0;i<n;i++)
  27. scanf("%lld%lld",&a[i].m,&a[i].s);
  28. sort(a,a+n,cmp);
  29. ll ans=0,sum=0;
  30. for(int i=0;i<n;i++)
  31. {
  32. q.push(a[i]);
  33. while(q.back().m-q.front().m>=d)
  34. {
  35. sum-=q.front().s;
  36. q.pop();
  37. }
  38. sum+=a[i].s;
  39. ans=max(ans,sum);
  40. }
  41. printf("%lld\n",ans);
  42. }
  43. return 0;
  44. }
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. using namespace std;
  6. #define N 100010
  7. #define ll long long
  8. struct node
  9. {
  10. ll m,s;
  11. }a[N];
  12. bool cmp(node x,node y)
  13. {
  14. if(x.m==y.m)
  15. return x.s<y.s;
  16. return x.m<y.m;
  17. }
  18. int main()
  19. {
  20. ll n,d;
  21. while(scanf("%lld%lld",&n,&d)!=EOF)
  22. {
  23. memset(a,0,sizeof(a));
  24. for(int i=0;i<n;i++)
  25. scanf("%lld%lld",&a[i].m,&a[i].s);
  26. sort(a,a+n,cmp);
  27. int j=0;
  28. ll ans=0,sum=0;
  29. for(int i=0;i<n;i++)
  30. {
  31. while(a[j].m-a[i].m<d)
  32. {
  33. sum+=a[j].s;
  34. ans=max(ans,sum);
  35. j++;
  36. if(j==n)
  37. break;
  38. }
  39. sum=sum-a[i].s;
  40. }
  41. printf("%lld\n",ans);
  42. }
  43. return 0;
  44. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注