Advertisement
mickypinata

CUBE-T193: Saengja

Dec 7th, 2021
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define f first
  5. #define s second
  6. typedef long long lli;
  7. typedef pair<int, int> pii;
  8. typedef pair<int, lli> pil;
  9.  
  10. const int N = 1e5 + 5;
  11.  
  12. lli dp[N];
  13. pii can[N];
  14.  
  15. int main(){
  16.  
  17.     int n, limWalk;
  18.     scanf("%d%d", &n, &limWalk);
  19.     for(int i = 1; i <= n; ++i){
  20.         scanf("%d%d", &can[i].f, &can[i].s);
  21.     }
  22.     sort(can + 1, can + n + 1);
  23.     deque<pil> dq;
  24.     dp[0] = 0;
  25.     for(int i = 1; i <= n; ++i){
  26.         while(!dq.empty() && dq.front().f < can[i].f - limWalk){
  27.             dq.pop_front();
  28.         }
  29.         dp[i] = dp[i - 1];
  30.         if(!dq.empty()){
  31.             dp[i] = max(dp[i], can[i].s - can[i].f + dq.front().s);
  32.         }
  33.         while(!dq.empty() && dq.back().s <= can[i].s + can[i].f + dp[i - 1]){
  34.             dq.pop_back();
  35.         }
  36.         dq.emplace_back(can[i].f, can[i].s + can[i].f + dp[i - 1]);
  37.     }
  38.     cout << dp[n];
  39.  
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement