Beingamanforever

Kefa and Company, Two Pointers

Dec 12th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-13 01:42:38
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.  
  28.     int n, d;
  29.     cin >> n >> d;
  30.     vpi a(n);
  31.     for (int i = 0; i < n; i++)
  32.     {
  33.         cin >> a[i].first >> a[i].second;
  34.     }
  35.     sort(all(a));
  36.     int mx = 0, cur = 0, l = 0;
  37.     for (int r = 0; r < n; r++)
  38.     {
  39.         cur += a[r].second;
  40.         while (a[r].first - a[l].first >= d)
  41.         {
  42.             cur -= a[l].second;
  43.             l++;
  44.         }
  45.         mx = max(mx, cur);
  46.     }
  47.     cout << mx << endl;
  48.     return;
  49. }
  50.  
  51. signed main()
  52. {
  53.     NeedForSpeed;
  54.     int t = 1;
  55.     // cin >> t;
  56.     while (t--)
  57.     {
  58.         solve();
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment