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.
尺取直接搞...
然后因为没开longlong wa了一发...
因为看错条件,money差值等于d也会被鄙视...所以不能取等号...
都是傻逼错误...药丸,药丸啊...
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /************************************************************************* 2 > File Name: code/cf/#321/B.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年09月25日 星期五 17时21分55秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 #define y1 hust111qqz 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 #define lr dying111qqz 26 using namespace std; 27 #define For(i, n) for (int i=0;i<int(n);++i) 28 typedef long long LL; 29 typedef double DB; 30 const int inf = 0x3f3f3f3f; 31 const int N=1E5+7; 32 LL n,d; 33 struct Q 34 { 35 LL m,f; 36 }q[N]; //没开long long ,再wa一发,sssssad 37 38 bool cmp(Q a,Q b) 39 { 40 if (a.m<b.m) return true; 41 return false; 42 } 43 44 void solve() 45 { 46 47 LL head = 0 ; 48 LL tail = 0 ; 49 LL sum = 0 ; 50 LL ans = -1 ; 51 while (head<n) 52 { 53 while (q[tail].m-q[head].m<d&&tail<n) //条件看错...不能等于d,怒wa一发 54 { 55 sum += q[tail].f; 56 // cout<<"sum:"<<sum<<endl; 57 tail++; 58 } 59 // cout<<"sum:"<<sum<<endl; 60 ans = max (ans,sum); 61 sum -= q[head].f; 62 head++; 63 64 } 65 cout<<ans<<endl; 66 } 67 int main() 68 { 69 #ifndef ONLINE_JUDGE 70 freopen("in.txt","r",stdin); 71 #endif 72 while(scanf("%I64d %I64d",&n,&d)!=EOF) 73 { 74 for ( int i = 0; i < n ; i++) cin>>q[i].m>>q[i].f; 75 sort(q,q+n,cmp); 76 // for ( int i = 0 ; i < n ; i++) cout<<q[i].m<<" "<<q[i].f<<endl; 77 solve(); 78 } 79 80 #ifndef ONLINE_JUDGE 81 fclose(stdin); 82 #endif 83 return 0; 84 }