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 are not choosing many complicated things. Since exactly numbers are negated, exactly one number keeps its original sign.
Try fixing the one number that stays unchanged. If that number is , every other contributes .
Let If is the only unnegated number, the final sum is
Rewrite the expression: Since is fixed, maximizing the answer only depends on maximizing .
Leave the largest number unchanged and negate the other six. The answer is simply Tiny formula, no DP monster hiding under the bed.
Negating out of integers means exactly one integer is not negated.
Suppose we leave unchanged. Let
Then the resulting sum is
But
so the result becomes
The value is fixed for the test case, so maximizing
is the same as maximizing .
So the optimal move is brutally simple: keep the largest number unchanged, and negate the other six.
Therefore the answer is
This also handles negative numbers correctly. If all numbers are negative, the "largest" one is the least negative, which is exactly the one we should leave alone.
For each test case, we scan numbers.
per test case, and
over all test cases. Memory usage is
#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 sum = 0;
int mx = -1000000000;
for (int i = 0; i < 7; i++) {
int x;
cin >> x;
sum += x;
mx = max(mx, x);
}
cout << 2 * mx - sum << '\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 sum = 0;
int mx = -1000000000;
for (int i = 0; i < 7; i++) {
int x;
cin >> x;
sum += x;
mx = max(mx, x);
}
cout << 2 * mx - sum << '\n';
}
return 0;
}