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!
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.
Print the maximum total friendship factir that can be reached.
4 5
75 5
0 100
150 20
75 1
100
5 100
0 7
11 32
99 10
46 8
87 54
111
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.
读题不清楚真是大问题,然后想二分可以搞,于是读错题的情况下调bug调的想死,然后放弃治疗,换一种方法。
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <algorithm> 8 #include <vector> 9 #include <cmath> 10 11 using namespace std; 12 13 const int maxn = 1000005; 14 15 typedef long long LL; 16 17 vector<int>G[maxn]; 18 19 20 struct node 21 { 22 int x,y; 23 }a[maxn]; 24 25 26 bool cmp(node a,node b) 27 { 28 if(a.x == b.x) return a.y<b.y; 29 return a.x<b.x; 30 } 31 32 33 34 int main() 35 { 36 int n,m; 37 38 while(scanf("%d%d",&n,&m)!=EOF){ 39 for(int i=1;i<=n;i++){ 40 scanf("%d%d",&a[i].x,&a[i].y); 41 } 42 43 sort(a+1,a+n+1,cmp); 44 LL sum = 0; 45 LL ans = -999999999999; 46 int l = 1; 47 for(int i=1;i<=n;i++){ 48 while(a[i].x-a[l].x>=m) 49 sum -= a[l++].y; 50 sum += a[i].y; 51 ans = max(sum,ans); 52 } 53 54 printf("%lld ",ans); 55 } 56 57 return 0; 58 }