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> all_pairs;
  59. vector<int> singles;
  60. each(p, counts) {
  61. rep(i, 0, p.ss / 2) {
  62. all_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 max_perimeter = 0;
  71.  
  72. // Case 0: 0 singles (even sides)
  73. multiset<int> current_pairs_0 = all_pairs;
  74. int p_even = 0;
  75. each(p, current_pairs_0) p_even += p;
  76. p_even *= 2;
  77. while (sz(current_pairs_0) >= 2) {
  78. int s_max = *current_pairs_0.rbegin();
  79. if (2 * s_max < p_even) {
  80. max_perimeter = max(max_perimeter, p_even);
  81. break;
  82. } else {
  83. p_even -= 2 * s_max;
  84. current_pairs_0.erase(prev(current_pairs_0.end()));
  85. }
  86. }
  87.  
  88. // Case 1: 1 single (odd sides)
  89. multiset<int> current_pairs_1 = all_pairs;
  90. int p_pairs_1 = 0;
  91. each(p, current_pairs_1) p_pairs_1 += p;
  92. p_pairs_1 *= 2;
  93. while (sz(current_pairs_1) >= 1) {
  94. int s_pair_max = *current_pairs_1.rbegin();
  95. bool found = false;
  96. each(s, singles) {
  97. int p_total = p_pairs_1 + s;
  98. int s_max = max(s_pair_max, s);
  99. if (2 * s_max < p_total) {
  100. max_perimeter = max(max_perimeter, p_total);
  101. found = true;
  102. break;
  103. }
  104. }
  105. if (found) break;
  106. p_pairs_1 -= 2 * s_pair_max;
  107. current_pairs_1.erase(prev(current_pairs_1.end()));
  108. }
  109.  
  110. // Case 2: 2 singles (even sides)
  111. if (sz(singles) >= 2) {
  112. multiset<int> current_pairs_2 = all_pairs;
  113. int p_pairs_2 = 0;
  114. each(p, current_pairs_2) p_pairs_2 += p;
  115. p_pairs_2 *= 2;
  116. while (sz(current_pairs_2) >= 1) {
  117. int s_pair_max = *current_pairs_2.rbegin();
  118. int s1 = singles[0];
  119. int s2 = singles[1];
  120. int p_total = p_pairs_2 + s1 + s2;
  121. int s_max = max(s_pair_max, s1);
  122. if (2 * s_max < p_total) {
  123. max_perimeter = max(max_perimeter, p_total);
  124. break;
  125. } else {
  126. p_pairs_2 -= 2 * s_pair_max;
  127. current_pairs_2.erase(prev(current_pairs_2.end()));
  128. }
  129. }
  130. }
  131.  
  132. cout << max_perimeter << endl;
  133. }
  134.  
  135. // Main
  136. int32_t main() {
  137. fast_io;
  138.  
  139. int t = 1;
  140. cin >> t;
  141. while (t--) {
  142. solve();
  143. }
  144.  
  145. return 0;
  146. }
Success #stdin #stdout 0s 5320KB
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