fork download
  1. #include <bits/stdc++.h>
  2. #define Hoshimachi signed
  3. #define Suisei main
  4. using namespace std;
  5. #define int long long
  6. const int N=1e6+5;
  7. int n,k,a[N];
  8. Hoshimachi Suisei(){
  9. ios_base::sync_with_stdio(0);
  10. cin.tie(0); cout.tie(0);
  11. freopen("CHIAHANG.inp","r",stdin);
  12. freopen("CHIAHANG.out","w",stdout);
  13. cin>>n>>k;
  14. for(int i=1; i<=n; i++){
  15. cin>>a[i];
  16. }
  17. sort(a+1,a+1+n,greater<int>());
  18. int ans=0;
  19. multiset<int> ms;
  20. for(int i=1; i<=n; i++){
  21. auto it=ms.lower_bound(a[i]+k);
  22. if(it==ms.end()){
  23. ans+=a[i];
  24. }
  25. else{
  26. ms.erase(it);
  27. }
  28. ms.insert(a[i]);
  29. }
  30. cout<<ans;
  31. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty