fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, m, k, a[1000005];
  6. long long pre[1000005];
  7.  
  8. int main() {
  9. cin >> m >> n >> k;
  10. for (int i = 1; i <= m; i++)
  11. cin >> a[i];
  12. sort(a + 1, a + m + 1);
  13.  
  14. for (int i = 1; i <= m; i++)
  15. pre[i] = pre[i - 1] + a[i];
  16.  
  17. long long Ans = -2;
  18.  
  19. for (int i = n; i <= m; i++) {
  20. /// goi ban thu i la cao nhat
  21. /// (i - 1, i - 2, i - 3, ... sao cho du n thang
  22. /// (i - n + 1)
  23. /// tap the cac ban [i - n + 1, i]
  24. if (a[i] - a[i - n + 1] <= k)
  25. Ans = max(Ans, pre[i] - pre[i - n]);
  26. }
  27. cout << Ans;
  28.  
  29. }
  30.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty