Solution
设三种颜色中最大数量为 ,将 升序排序,总和为 。
对于任意颜色分配,最大配对总美观度的最优值为:
- 若 :需要从最大的 个数中选出最小的 个,其和为 ,则答案 = 。
- 若 :
- 为偶数:答案 = 。
- 为奇数:答案 = ( 为最小值)。
当 时,要使 最大,应让最大的 个数作为颜色最多的那类花。 因此直接按上述公式计算即可。
AC Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n, x, y, z;
cin >> n >> x >> y >> z;
vector<ll> a(n);
for (int i = 0; i < n; ++i){
cin >> a[i];
}
sort(a.begin(), a.end());
ll sum = 0;
for (ll v : a) sum += v;
ll c = max({x, y, z});
ll ans;
if (2 * c > n) {
ll k = 2 * c - n;
ll sub = 0;
for (int i = n - c; i < n - c + k; ++i) sub += a[i];
ans = sum - sub;
}
else {
if (n & 1) ans = sum - a[0];
else ans = sum;
}
cout << ans << "\n";
}
return 0;
}