fork download
  1. // C. Kefa and Company
  2.  
  3.  
  4. #pragma GCC target("avx2")
  5. #pragma GCC optimization("O3")
  6. #pragma GCC optimization("unroll-loops")
  7.  
  8. #include<bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. typedef long long ll;
  13. typedef long double ld;
  14. typedef pair<ll, ll> pll;
  15. typedef vector<bool> vb;
  16. typedef vector<int> vi;
  17. typedef vector<ll> vll;
  18. typedef vector<vi> vvi;
  19. typedef vector<vb> vvb;
  20. typedef vector<vll> vvll;
  21. typedef vector<pll> vpll;
  22. typedef vector<string> vs;
  23. typedef unordered_map<ll, ll> umll;
  24. template<class T>
  25. using pq = priority_queue<T, vector<T>, greater<T>>;
  26.  
  27. #define io \
  28.   ios_base::sync_with_stdio(false); \
  29.   cin.tie(nullptr);
  30.  
  31. void solve() {
  32. int n, d;
  33. cin >> n >> d;
  34. vector<pair<int, int>> v(n);
  35. for (int i = 0; i < n; ++i) {
  36. cin >> v[i].first >> v[i].second;
  37. }
  38. sort(v.begin(), v.end());
  39. ll sum = 0, ans = INT_MIN, l = 0, r = 0;
  40. while (r < n) {
  41. if (v[r].first - v[l].first < d) {
  42. sum += v[r].second;
  43. r++;
  44. } else {
  45. sum -= v[l].second;
  46. l++;
  47. }
  48. ans = max(ans, sum);
  49. }
  50. cout << ans;
  51. }
  52.  
  53. int main() {
  54. io;
  55. ll tests = 1;
  56. // cin >> tests;
  57. while (tests--) {
  58. solve();
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Standard output is empty