fork download
  1. /*
  2.  * Author: nerdyninja
  3.  * TCQF181B
  4.  */
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. ios_base::sync_with_stdio(0);
  12. // Start Solution here
  13. int t;
  14. cin >> t;
  15. while(t--){
  16. int n;
  17. long long y;
  18. cin >> n >> y;
  19. long long a[n];
  20. for (int i = 0; i < n; i++)cin >> a[i];
  21. sort(a, a+n);
  22. long long median = a[n/2];
  23. if((n - n/2) >= y)cout << "0" << endl;
  24. else{
  25. int count = n - n/2;
  26. int k = n/2 - 1;
  27. long long marksAdded = 0;
  28. while(count < y && k >= 0){
  29. marksAdded += median - a[k];
  30. k--;
  31. count++;
  32. }
  33. if(count >= y)cout << marksAdded << endl;
  34. else cout << "-1" << endl;
  35. }
  36. }
  37. // End Solution here
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1