fork download
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstdlib>
  18. #include <ctime>
  19. #include <string>
  20. #include <cstring>
  21. #include <string.h>
  22.  
  23. using namespace std;
  24. typedef long long LL;
  25.  
  26. int main() {
  27. int n, d;
  28. cin >> n >> d;
  29. vector<pair<int,int>> v(n);
  30. for (int i = 0; i < n; i++)
  31. {
  32. int m, f;
  33. cin >> m >> f;
  34. v[i] = make_pair(m, f);
  35. }
  36. sort(v.begin(), v.end());
  37. vector<LL> t(n);
  38. for (int i = 0; i < n; i++)
  39. {
  40. t[i] = v[i].second;
  41. }
  42. for (int i = n-2; i >= 0; i--)
  43. {
  44. t[i] += t[i+1];
  45. }
  46. LL best = 0;
  47. for (int i = 0; i < n; i++)
  48. {
  49. int x = v[i].first+d-1;
  50. pair<int,int> z = make_pair(x, 2000000000);
  51. int p = upper_bound(v.begin(), v.end(), z) - v.begin();
  52. LL ff = (p == n) ? t[i] : t[i]-t[p];
  53. best = max(ff, best);
  54. }
  55. cout << best << endl;
  56. return 0;
  57. }
Success #stdin #stdout 0s 3468KB
stdin
5 100
0 7
11 32
99 10
46 8
87 54
stdout
111