fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Speed
  5. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  6.  
  7. // Typedefs
  8. #define int long long
  9. #define pb push_back
  10. #define ff first
  11. #define ss second
  12. #define all(x) (x).begin(), (x).end()
  13. #define rall(x) (x).rbegin(), (x).rend()
  14. #define sz(x) ((int)(x).size())
  15. #define endl '\n'
  16. #define yes cout << "YES\n"
  17. #define no cout << "NO\n"
  18.  
  19. // Loops
  20. #define rep(i,a,b) for(int i=a;i<b;++i)
  21. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  22. #define each(x, a) for (auto& x : a)
  23.  
  24. // Consts
  25. const int INF = 1e18;
  26. const int MOD = 1e9+7;
  27. const int N = 2e5 + 5;
  28.  
  29. // Math
  30. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  31. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  32.  
  33. int power(int a, int b, int m = MOD) {
  34. int res = 1;
  35. while (b > 0) {
  36. if (b & 1) res = res * a % m;
  37. a = a * a % m;
  38. b >>= 1;
  39. }
  40. return res;
  41. }
  42.  
  43. int modinv(int a, int m = MOD) {
  44. return power(a, m - 2, m);
  45. }
  46.  
  47. // Logic
  48. void solve() {
  49. int n;
  50. cin >> n;
  51. map<int, int> counts;
  52. int total_sum = 0;
  53. rep(i, 0, n) {
  54. int val;
  55. cin >> val;
  56. counts[val]++;
  57. total_sum += val;
  58. }
  59.  
  60. vector<pair<int, int>> sorted_counts;
  61. each(p, counts) {
  62. sorted_counts.pb(p);
  63. }
  64.  
  65. multiset<int> kept_singles;
  66. multiset<int> pruned_singles;
  67. int pruned_sum = 0;
  68.  
  69. vector<int> initial_singles;
  70. each(p, counts) {
  71. if (p.ss % 2 != 0) initial_singles.pb(p.ff);
  72. }
  73. sort(rall(initial_singles));
  74.  
  75. rep(i, 0, sz(initial_singles)) {
  76. if (i < 2) {
  77. kept_singles.insert(initial_singles[i]);
  78. } else {
  79. pruned_singles.insert(initial_singles[i]);
  80. pruned_sum += initial_singles[i];
  81. }
  82. }
  83.  
  84. per(i, 0, sz(sorted_counts)) {
  85. int s_max_candidate = sorted_counts[i].ff;
  86.  
  87. int p_candidate = total_sum - pruned_sum;
  88. if (p_candidate > 2 * s_max_candidate) {
  89. cout << p_candidate << endl;
  90. return;
  91. }
  92.  
  93. int L = sorted_counts[i].ff;
  94. int C = sorted_counts[i].ss;
  95. total_sum -= L * C;
  96.  
  97. if (C % 2 != 0) {
  98. auto it_kept = kept_singles.find(L);
  99. if (it_kept != kept_singles.end()) {
  100. kept_singles.erase(it_kept);
  101. } else {
  102. auto it_pruned = pruned_singles.find(L);
  103. pruned_sum -= *it_pruned;
  104. pruned_singles.erase(it_pruned);
  105. }
  106.  
  107. if (sz(kept_singles) < 2 && !pruned_singles.empty()) {
  108. int to_move = *pruned_singles.rbegin();
  109. pruned_sum -= to_move;
  110. pruned_singles.erase(prev(pruned_singles.end()));
  111. kept_singles.insert(to_move);
  112. }
  113. }
  114. }
  115.  
  116. cout << 0 << endl;
  117. }
  118.  
  119. // Main
  120. int32_t main() {
  121. fast_io;
  122.  
  123. int t = 1;
  124. cin >> t;
  125. while (t--) {
  126. solve();
  127. }
  128.  
  129. return 0;
  130. }
Success #stdin #stdout 0.01s 5308KB
stdin
5
3
5 5 7
3
4 5 7
3
5 5 10
7
4 3 5 1 5 3 3
4
2 3 5 7
stdout
17
0
0
23
0