fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. void solve(){
  6. int t,n,k,in;
  7. vector<int>v;
  8. cin>>t;
  9. while(t--){
  10. cin>>n>>k;
  11. for(int i=0;i<n;i++){
  12. cin>>in;
  13. v.push_back(in);
  14. }
  15. sort(v.begin(),v.end());
  16.  
  17. vector<long long>prefix(n+1,0);
  18. prefix[0]=v[0];
  19. for(int i=1;i<=n;i++){
  20. prefix[i]=prefix[i-1]+v[i];
  21. }
  22.  
  23. vector<long long> result(n);
  24. for (int m = 1; m <n; m++) {
  25. int free_items = (m - 1) / (k + 1);
  26. int paid_items = m - free_items;
  27. result[m - 1] = prefix[paid_items];
  28. }
  29. for (int i = 0; i < n; i++) {
  30. cout << result[i] << " ";
  31. }
  32. cout << "\n";
  33.  
  34.  
  35.  
  36.  
  37. }
  38.  
  39.  
  40.  
  41.  
  42. }
  43.  
  44. int main() {
  45. ios_base::sync_with_stdio(false);
  46. cin.tie(nullptr);
  47. solve();
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5280KB
stdin
2
 5 1
 4 1 6 10 2
 3 2
 1 1 1
stdout
3 7 7 13 0 
2 3 0