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. 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. per(i, 0, sz(sorted_counts)) {
  66. int current_s_max = sorted_counts[i].ff;
  67.  
  68. int p_candidate = total_sum;
  69. vector<int> current_singles;
  70.  
  71. rep(j, 0, i + 1) {
  72. if (sorted_counts[j].ss % 2 != 0) {
  73. current_singles.pb(sorted_counts[j].ff);
  74. }
  75. }
  76.  
  77. if (sz(current_singles) > 2) {
  78. sort(all(current_singles));
  79. int to_remove = sz(current_singles) - 2;
  80. rep(j, 0, to_remove) {
  81. p_candidate -= current_singles[j];
  82. }
  83. }
  84.  
  85. if (p_candidate > 2 * current_s_max) {
  86. cout << p_candidate << endl;
  87. return;
  88. }
  89.  
  90. total_sum -= sorted_counts[i].ff * sorted_counts[i].ss;
  91. }
  92.  
  93. cout << 0 << endl;
  94. }
  95.  
  96. // Main
  97. int32_t main() {
  98. fast_io;
  99.  
  100. int t = 1;
  101. cin >> t;
  102. while (t--) {
  103. solve();
  104. }
  105.  
  106. return 0;
  107. }
Success #stdin #stdout 0s 5328KB
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