Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main()
- {
- long long a,b;
- cin>>a>>b;
- vector<pair<long long,long long>>v;
- long long x,y;
- for(long long i=0;i<a;i++)
- {
- cin>>x>>y;
- v.push_back({x,y});
- }
- sort(v.begin(),v.end());
- long long z[a];
- long long zbir=0;
- for(long long i=0;i<a;i++)
- {
- zbir+=v[i].second;
- z[i]=zbir;
- }
- long long l,r,mid;
- long long maxvalue=0;
- for(long long i=0;i<a;i++)
- {
- l=i;
- r=a-1;
- mid=(l+r)/2;
- long long najgolem=-1;
- while(l<=r)
- {
- mid=(l+r)/2;
- if(v[mid].first-v[i].first>=b)
- {
- r=mid-1;
- }
- else
- {
- najgolem=mid;
- l=mid+1;
- }
- }
- if(najgolem + 1 < a && v[najgolem+1].first-v[i].first<b)
- {
- najgolem++;
- }
- if(i>0)
- maxvalue=max(maxvalue, (z[najgolem]-z[i-1]));
- else if(i==0)
- maxvalue=max(maxvalue, z[najgolem]);
- }
- cout<<maxvalue<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement