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.
You can always take the whole remaining multiset with sum, or just take every element by sum one by one. So the answer is at least the ordinary total sum.
Think of using mex as replacing the normal contribution of the chosen subset. A subset is useful only if .
If a mex subset contains some number larger than its mex, that number does not help the mex but does increase the sum you gave up. So useful mex subsets only need small required values.
For mex , the subset must contain . The gain over summing those elements is Check which actually gives positive gain.
Only two shapes matter: gives gain , and also gives gain . Since every useful mex operation consumes exactly one zero, the best move is simply: add the total sum, then add for every zero in the multiset.
The first thing to notice is that sum gives us a boring but very strong baseline: if we never use mex, the score is
So the whole problem is really: when does a mex operation beat just summing those same elements?
Suppose we choose some subset and use mex on it. Compared to using sum on the same elements, the extra profit is
We only care about subsets where this is positive. Otherwise, just sum the elements and move on with life.
What can a useful mex subset look like?
Let . Then must contain every number
and it must not contain .
Any element greater than does not help increase the mex. It only increases , which makes the mex operation worse compared to summing. So an optimal useful mex subset never needs random big numbers stuffed inside. That would be self-sabotage, which is usually bad outside of Codeforces too.
The cheapest subset with mex is exactly one copy of each of
Its sum is
So the best possible gain for mex is
Now check small values:
So mex can only improve the answer by , and every improving mex operation needs exactly one zero.
That gives a clean upper bound: if there are zeroes, the answer is at most
Can we always achieve that? Yep. For every zero, take the subset and use mex, gaining instead of the zero's normal sum contribution of . Then sum all remaining elements normally.
So the maximum score is simply:
That's it. Count zeroes, add them to the sum. Very 800-rated, very "wait, that's the whole problem?"
#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;
int ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ans += x;
if (x == 0) ans++;
}
cout << ans << '\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;
int ans = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ans += x;
if (x == 0) ans++;
}
cout << ans << '\n';
}
return 0;
}