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.
Even fields are boring in the best way: they never change the mower state. They only care whether the mower is already on when FJ visits them.
Only odd fields can toggle the mower. Starting from off, the 1st odd field visited is cut, the 2nd odd field is not cut, the 3rd is cut, and so on.
If there is at least one odd field, FJ can turn the mower on with some odd field, then visit all even fields while it stays on. So every even field can be cut whenever an odd field exists.
Among the odd fields, exactly of them can be cut, where is the number of odd fields. The order lets you choose which odd fields those are.
So the answer is: if there are no odd values, output . Otherwise, add all even values, then sort the odd values descending and add the largest of them.
Let’s strip away the story and look at the machine.
The mower starts off. For each field:
So the only fields that actually control anything are the odd ones.
Even fields
An even field never changes the mower state. That means if the mower is off, visiting an even field cuts nothing. If the mower is on, visiting an even field cuts all of it.
So if there are no odd fields at all, the mower never turns on. The answer is just . Brutal, but fair.
If there is at least one odd field, we can visit an odd field first. Since the mower starts off, that first odd field toggles it on and gets cut. After that, we can immediately visit every even field, and since evens do not toggle anything, they all get cut.
Therefore:
Odd fields
Now suppose there are odd fields.
Ignore evens for a second. Every odd field toggles the mower. Starting off:
So among the odd fields, exactly the fields in odd positions of the odd-field order are cut.
That count is:
The important greedy bit: FJ controls the order, so he controls which odd fields land in those cut positions. There is no hidden constraint here. Put the biggest odd values in the cut positions, and dump the smaller odd values into the off-toggle positions. No need to overthink it; this is a sorting problem wearing a fake mustache.
So we should cut the largest odd values.
Final formula
Let:
odd be the list of all odd ;If , answer is:
Otherwise, answer is:
Implementation details:
long long, because sums can exceed 32-bit int.(k + 1) / 2 odd values.Time complexity per test case is because of sorting the odd values. Across all test cases, this is easily fine for .
#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;
ll evenSum = 0;
vector<ll> odd;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
if (x % 2 == 0) evenSum += x;
else odd.push_back(x);
}
if (odd.empty()) {
cout << 0 << '\n';
continue;
}
sort(odd.rbegin(), odd.rend());
ll ans = evenSum;
int take = (int)(odd.size() + 1) / 2;
for (int i = 0; i < take; i++) ans += odd[i];
cout << ans << '\n';
}
}#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;
ll evenSum = 0;
vector<ll> odd;
for (int i = 0; i < n; i++) {
ll x;
cin >> x;
if (x % 2 == 0) evenSum += x;
else odd.push_back(x);
}
if (odd.empty()) {
cout << 0 << '\n';
continue;
}
sort(odd.rbegin(), odd.rend());
ll ans = evenSum;
int take = (int)(odd.size() + 1) / 2;
for (int i = 0; i < take; i++) ans += odd[i];
cout << ans << '\n';
}
}