Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Task : _example
- Author : Phumipat C. [MAGCARI]
- Language: C++
- Created : 11 December 2022 [11:55]
- */
- #include<bits/stdc++.h>
- using namespace std;
- struct A{
- long long value;
- int idx;
- bool operator < (const A&o) const{
- return value<o.value;
- }
- };
- pair<int ,int > restaurant[100010];
- // first is position
- // second is happy value
- priority_queue<A > heap;
- int main(){
- int n,k;
- scanf("%d %d",&n,&k);
- for(int i=1;i<=n;i++)
- scanf("%d %d",&restaurant[i].first,&restaurant[i].second);
- sort(restaurant+1,restaurant+n+1);
- long long ans = 0;
- for(int i=1;i<=n;i++){
- int xj = restaurant[i].first;
- int cj = restaurant[i].second;
- long long temp = ans;
- while(!heap.empty() && xj - heap.top().idx > k) heap.pop();
- if(!heap.empty()) ans = max(ans,heap.top().value+cj-xj);
- heap.push({temp+xj+cj,xj});
- }
- printf("%lld\n",ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment