fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #define ll long long int
  4. using namespace std;
  5.  
  6.  
  7. struct node{
  8. ll m,s;
  9. };
  10.  
  11. bool f(node n1,node n2){
  12. return n1.m<n2.m;
  13. }
  14.  
  15. int main() {
  16. // your code goes here
  17. int N,D;
  18. cin>>N>>D;
  19. node n[N];
  20. for(int i =0;i<N;i++){
  21. cin>>n[i].m>>n[i].s;
  22. }
  23. sort(n,n+N,f);
  24. ll l_max = 0,g_max = 0,sum = 0;
  25. for(int i =0;i<N;i++){
  26. sum+=n[i].s;
  27. }
  28. l_max = sum;
  29. for(int i =0;i<N;i++){
  30. int j = N-1;
  31. if(i>0){
  32. l_max = sum - n[i-1].s;
  33. sum = sum - n[i-1].s;
  34. }
  35. else{
  36. l_max = sum;
  37. }
  38. while(n[j].m-n[i].m >= D && j>i){
  39. l_max = l_max - n[j].s;
  40. j--;
  41. }
  42. if(g_max<l_max){
  43. g_max = l_max;
  44. }
  45. }
  46. cout<<g_max<<endl;
  47. return 0;
  48. }
Success #stdin #stdout 0s 3464KB
stdin
5 100
0 7
11 32
99 10
46 8
87 54
stdout
111