fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. typedef long long ll;
  5.  
  6. void solve() {
  7. int n, c;
  8. cin >> n >> c;
  9.  
  10. vector<int> a(n);
  11.  
  12. int k{};
  13.  
  14. for(int i{}; i < n; ++i){
  15. cin >> a[i];
  16. if(a[i] < c) k++;
  17. }
  18.  
  19. sort(a.begin(), a.end());
  20.  
  21. ll score{};
  22.  
  23. // 2 cases : k <= n/2
  24. // k > n/2
  25.  
  26. if(k > n/2){
  27. for(int i{n/2}; i < n; ++i){
  28. score += (a[i]-c);
  29. }
  30. }
  31.  
  32. else{
  33. for(int i{k}; i < n; ++i){
  34. score += (a[i] - c);
  35. }
  36. }
  37.  
  38. cout << score << endl;
  39.  
  40.  
  41. }
  42.  
  43. int main() {
  44. ios::sync_with_stdio(false);
  45. cin.tie(nullptr);
  46.  
  47. int t;
  48. cin >> t;
  49. while (t--) {
  50. solve();
  51. }
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 5316KB
stdin
5
1 0
-1
3 0
1 3 1
3 2
1 3 1
6 6
3 1 4 1 5 9
4 1000000000
-1000000000 -1000000000 -1000000000 -1000000000
stdout
-1
5
0
0
-4000000000