Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
After every action, the bag is either empty or has an odd sum. If it ever becomes even, it instantly gets reset to .
Look only at the coins that survive in the bag at the end. The first surviving coin must be odd; after that, adding even coins is safe, but adding another odd coin wipes the bag.
So any positive final score is exactly one chosen odd coin plus some chosen even coins. Everything before that final segment is just discard material.
For a fixed number of surviving even coins, use the largest odd coin and the largest evens. Since all values are positive, you want as many surviving evens as feasibility allows.
If the final bag uses one odd and evens, then the other selected coins must contain an even number of odd coins so they can be discarded. Equivalently, among the selected coins, the number of odd coins must be odd.
Key observation
After each action, the bag sum is either or odd. If it becomes even, the cat clears it immediately.
Now suppose the final score is positive. Consider only the coins still in the bag after the last reset.
This final surviving segment starts from an empty bag. The first surviving coin cannot be even, because an even coin alone makes the sum even and gets deleted instantly. So it must be odd.
After that, the bag sum is odd:
Therefore, the final bag must contain:
That is the whole trick. Very small, very annoying, very Codeforces.
What can be discarded before the final bag?
Before the final surviving segment, the bag must end empty. Which coins can be thrown away like that?
Even coins are easy: from an empty bag, choosing one even coin immediately makes an even sum, so it gets cleared.
Odd coins must be discarded in pairs: odd plus odd gives an even sum, then the bag gets cleared.
So the discarded part is possible exactly when it contains an even number of odd coins.
The final bag itself contains one odd coin. Therefore, among the total selected coins, the number of selected odd coins must be odd.
Choosing coins
Let:
For a fixed , suppose we select odd coins. Then:
Only one odd coin survives, so we always make the largest odd coin survive. The other selected odd coins are sacrificed before the final reset, so their values do not matter.
The selected even coins can all survive in the final bag, so we take the largest even coins.
Since all values are positive, we want to maximize , which means minimizing feasible odd .
So for each :
because we need at least one odd, and we cannot select more than evens.
If is even, increase it by to make it odd.
If this final is greater than or greater than , there is no way to end with a positive score, so the answer is .
Otherwise:
Algorithm
Sorting dominates, so the complexity is per test case. Across all tests, this is easily within limits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<ll> odd, even;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
if (x & 1) odd.push_back(x);
else even.push_back(x);
}
sort(odd.rbegin(), odd.rend());
sort(even.rbegin(), even.rend());
int o = (int)odd.size();
int m = (int)even.size();
vector<ll> pref(m + 1, 0);
for (int i = 0; i < m; i++) {
pref[i + 1] = pref[i] + even[i];
}
for (int k = 1; k <= n; k++) {
ll ans = 0;
if (o > 0) {
int x = max(1, k - m);
if (x % 2 == 0) x++;
if (x <= o && x <= k) {
int keptEven = k - x;
ans = odd[0] + pref[keptEven];
}
}
cout << ans << ' ';
}
cout << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<ll> odd, even;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
if (x & 1) odd.push_back(x);
else even.push_back(x);
}
sort(odd.rbegin(), odd.rend());
sort(even.rbegin(), even.rend());
int o = (int)odd.size();
int m = (int)even.size();
vector<ll> pref(m + 1, 0);
for (int i = 0; i < m; i++) {
pref[i + 1] = pref[i] + even[i];
}
for (int k = 1; k <= n; k++) {
ll ans = 0;
if (o > 0) {
int x = max(1, k - m);
if (x % 2 == 0) x++;
if (x <= o && x <= k) {
int keptEven = k - x;
ans = odd[0] + pref[keptEven];
}
}
cout << ans << ' ';
}
cout << '\n';
}
return 0;
}