MAGCARI

Saengja

Dec 10th, 2022
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 11 December 2022 [11:55]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     long long value;
  11.     int idx;
  12.     bool operator < (const A&o) const{
  13.         return value<o.value;
  14.     }
  15. };
  16. pair<int ,int > restaurant[100010];
  17. // first is position
  18. // second is happy value
  19. priority_queue<A > heap;
  20. int main(){
  21.     int n,k;
  22.     scanf("%d %d",&n,&k);
  23.     for(int i=1;i<=n;i++)
  24.         scanf("%d %d",&restaurant[i].first,&restaurant[i].second);
  25.     sort(restaurant+1,restaurant+n+1);
  26.     long long ans = 0;
  27.     for(int i=1;i<=n;i++){
  28.         int xj = restaurant[i].first;
  29.         int cj = restaurant[i].second;
  30.         long long temp = ans;
  31.         while(!heap.empty() && xj - heap.top().idx > k) heap.pop();
  32.         if(!heap.empty())   ans = max(ans,heap.top().value+cj-xj);
  33.         heap.push({temp+xj+cj,xj});
  34.     }
  35.     printf("%lld\n",ans);
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment