fork(1) 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. rep(i, 0, n) {
  53. int val;
  54. cin >> val;
  55. counts[val]++;
  56. }
  57.  
  58. multiset<int> pairs;
  59. vector<int> singles;
  60. each(p, counts) {
  61. rep(i, 0, p.ss / 2) {
  62. pairs.insert(p.ff);
  63. }
  64. if (p.ss % 2 == 1) {
  65. singles.pb(p.ff);
  66. }
  67. }
  68. sort(rall(singles));
  69.  
  70. int ans1 = 0, ans2 = 0;
  71.  
  72. if (!pairs.empty()) {
  73. int p_pairs = 0;
  74. each(p, pairs) p_pairs += p;
  75. p_pairs *= 2;
  76. int s_pair_max = *pairs.rbegin();
  77.  
  78. each(s, singles) {
  79. int p_total = p_pairs + s;
  80. int s_max = max(s_pair_max, s);
  81. if (2 * s_max < p_total) {
  82. ans1 = p_total;
  83. break;
  84. }
  85. }
  86. }
  87.  
  88. int p_even = 0;
  89. each(p, pairs) p_even += p;
  90. p_even *= 2;
  91.  
  92. while (sz(pairs) >= 2) {
  93. int s_max = *pairs.rbegin();
  94. if (2 * s_max < p_even) {
  95. ans2 = p_even;
  96. break;
  97. } else {
  98. p_even -= 2 * s_max;
  99. pairs.erase(prev(pairs.end()));
  100. }
  101. }
  102.  
  103. cout << max(ans1, ans2) << endl;
  104. }
  105.  
  106. // Main
  107. int32_t main() {
  108. fast_io;
  109.  
  110. int t = 1;
  111. cin >> t;
  112. while (t--) {
  113. solve();
  114. }
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0.01s 5324KB
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
20
0