题解:P16494 樱蕊初含雪,惜春时易逝
Solution
设三种颜色中最大数量为
对于任意颜色分配,最大配对总美观度的最优值为:
- 若
2c > n :需要从最大的c 个数中选出最小的k = 2c - n 个,其和为sub ,则答案 =sum - sub 。 - 若
2c \le n : -
当
因此直接按上述公式计算即可。
::::success[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;
}
::::