fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. //const long long MAXN = 1en + 1;
  5. //ll arr[MAXN];
  6. //───────────────────────────────────────────────────────────────────────────
  7. signed main() {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(0); cout.tie(0);
  10. freopen("prize.inp","r",stdin);
  11. freopen("prize.out","w",stdout);
  12. //
  13. ll n,k; cin >> n >> k;
  14. ll a[n]; for (ll &i : a) cin >> i;
  15. sort(a, a + n);
  16. vector<bool> ben_trong(n, false);
  17. for (ll p = n - 1; p >= 0; p--) {
  18. if (ben_trong[p]) continue;
  19. ll current_pos = p;
  20. while(true) {
  21. ll i = current_pos - 1;
  22. while (i >= 0 && (ben_trong[i] || a[i] + k > a[current_pos])) i--;
  23. if (i < 0) break;
  24. ben_trong[i] = true;
  25. current_pos = i;
  26. }
  27. }
  28. ll ans = 0;
  29. for (ll i = 0; i < n; i++) {
  30. if (ben_trong[i]) continue;
  31. else {
  32. ans += a[i];
  33. }
  34. }
  35. cout << ans;
  36. //
  37. return 0;
  38. }
  39. //───────────────────────────────────────────────────────────────────────────
Success #stdin #stdout 3.78s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty