fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(false);
  6. cin.tie(NULL);
  7.  
  8. int n;
  9. long long k;
  10. if (!(cin >> n >> k)) return 0;
  11.  
  12. vector<pair<long long, long long>> a(n);
  13. for (int i = 0; i < n; i++) {
  14. cin >> a[i].first >> a[i].second;
  15. }
  16.  
  17. sort(a.begin(), a.end());
  18.  
  19. long long MAx = 0;
  20. long long crnt = 0;
  21. int j = 0;
  22.  
  23. for (int i = 0; i < n; i++) {
  24. while (j < n && a[j].first - a[i].first <= 2 * k) {
  25. crnt += a[j].second;
  26. j++;
  27. }
  28. MAx = max(MAx, crnt);
  29.  
  30. crnt -= a[i].second;
  31. }
  32.  
  33. cout << MAx << "\n";
  34. return 0;
  35. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Standard output is empty