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.
Don’t try to track every possible score. The operations only care about the previous score in two very simple ways: one likes large , the other likes small .
For the red card, the new score is . If you want this as large as possible, you should use the largest score reachable before this turn.
For the blue card, the new score is . This flips the sign of , so to make it large, you want the smallest score reachable before this turn. Tiny values suddenly become weapons. Very rude, very useful.
Maintain two values after each turn: = maximum reachable score and = minimum reachable score. Both are needed because blue turns maximum into minimum-ish behavior.
The transitions are:
Start with , process all turns, and answer .
We need maximize the final score after forced choices. On turn , from a current score :
The obvious brute force has strategies, which is impressively dead on arrival. So we need to compress the reachable scores.
Key Observation
At any turn, we do not need every reachable score. We only need the current maximum and minimum reachable scores.
Why?
For a fixed turn :
Red: increases when increases. So the best red result comes from the previous maximum score.
Blue: decreases when increases. So the best blue result comes from the previous minimum score.
That means the next maximum is determined only by the previous maximum and previous minimum:
But we also need the next minimum, because a terrible score can become amazing later through a blue card. That is the whole trick; negative scores are not trash, they are just potential slingshots.
For the minimum:
So:
Initialization
Before any turns, the only reachable score is , so:
Then process each pair and update both values at the same time. Do not update first and then use the new value for ; that would mix turns and create fake strategies. Store the new values separately.
Why this is enough
Suppose after some turn the set of reachable scores is huge. For the next turn, every possible next score is created by one of these two functions:
Function is increasing, so its maximum and minimum over all reachable happen at the old maximum and old minimum.
Function is decreasing, so its maximum and minimum happen at the old minimum and old maximum.
Therefore, knowing only preserves exactly the information needed for all future extrema. No hidden middle value can suddenly beat both endpoints, because these functions are linear and monotonic. Middle values can sit down.
Algorithm
For each test case:
Complexity
Each turn is processed once.
Time complexity:
per test case.
Memory complexity:
if storing both arrays, though the DP itself is . With the given input format, storing arrays is clean and totally fine.
Values can reach around , so use long long. int would explode and then gaslight you with wrong answers.
#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> a(n), b(n);
for (ll &x : a) cin >> x;
for (ll &x : b) cin >> x;
ll mn = 0, mx = 0;
for (int i = 0; i < n; i++) {
ll newMx = max(mx - a[i], b[i] - mn);
ll newMn = min(mn - a[i], b[i] - mx);
mx = newMx;
mn = newMn;
}
cout << mx << '\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> a(n), b(n);
for (ll &x : a) cin >> x;
for (ll &x : b) cin >> x;
ll mn = 0, mx = 0;
for (int i = 0; i < n; i++) {
ll newMx = max(mx - a[i], b[i] - mn);
ll newMn = min(mn - a[i], b[i] - mx);
mx = newMx;
mn = newMn;
}
cout << mx << '\n';
}
return 0;
}