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.
Think of each index as a tiny segment . Its current contribution is just the segment length.
When Ali chooses two indices, Bahamin only cares about the four numbers in those two segments. To maximize two absolute differences from four numbers, pair the smallest with the largest and the two middle numbers together.
For two segments, this operation increases the total by if the segments intersect. If they are disjoint with gap , it increases the total by exactly .
Ali only needs to make one “important” choice. Since , he can choose the same pair every round; after Bahamin has maximized that pair once, choosing it again changes nothing.
So the answer is the initial sum plus times the minimum gap between any two disjoint segments. If any two segments overlap, the minimum gap is . Sort segments by left endpoint and scan adjacent segments to find that minimum.
Let’s strip the game down to what it actually is.
For every index , replace the pair with the segment
Its contribution to the answer is
So the starting value is
Now we need to understand exactly what happens when Ali picks two indices.
What Bahamin can do to two indices
Suppose Ali picks indices and . Bahamin gets the four numbers
and can rearrange them into two new pairs. To maximize the sum of two absolute differences, sort those four numbers:
The best possible value is
That is: pair the smallest with the largest, and the two middle values together. Pairing big with small is the whole trick. Very subtle, very evil, very Codeforces.
Now compare that to the old value of just these two indices:
There are two cases.
Case 1: the two segments overlap
If and intersect, then after sorting the four endpoints, the maximized value is exactly the same as the old total.
Example shape:
Old value:
Bahamin’s max value:
These are equal after expanding. So the gain is .
Case 2: the two segments are disjoint
Say
The gap between them is
Old value:
Bahamin’s max value:
Subtract old from new:
So selecting two disjoint segments with gap increases the total by exactly .
That means every possible first move has a cost to Ali:
Ali wants the smallest possible gain.
Why basically does not matter
This is the part where the problem tries to look scarier than it is.
Since , Ali must choose some pair at least once. Bahamin then maximizes that pair. After that, if Ali chooses the same pair again, Bahamin cannot make it any worse: the two selected indices are already arranged in the maximum possible way for those four numbers.
So Ali can just pick his best pair in every round. Extra rounds do not force him to touch more indices. Nice try, statement.
Therefore the final answer is
Equivalently:
If any two segments overlap, the minimum gap is , and the answer is just .
Finding the minimum gap
We have segments .
Sort them by .
While scanning from left to right, keep the largest right endpoint seen so far, call it .
For the current segment :
Why is enough? Among all previous segments, the one with the largest right endpoint gives the smallest gap to the current left endpoint. Anything ending earlier is worse.
So the scan gives the minimum gap in after sorting.
Total complexity per test case:
which is completely 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, k;
cin >> n >> k;
vector<ll> a(n), b(n);
for (ll &x : a) cin >> x;
for (ll &x : b) cin >> x;
vector<pair<ll, ll>> seg(n);
ll ans = 0;
for (int i = 0; i < n; i++) {
ll l = min(a[i], b[i]);
ll r = max(a[i], b[i]);
seg[i] = {l, r};
ans += r - l;
}
sort(seg.begin(), seg.end());
ll bestGap = LLONG_MAX;
ll mxRight = seg[0].second;
for (int i = 1; i < n; i++) {
ll l = seg[i].first;
ll r = seg[i].second;
if (l <= mxRight) {
bestGap = 0;
break;
}
bestGap = min(bestGap, l - mxRight);
mxRight = max(mxRight, r);
}
cout << ans + 2 * bestGap << '\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, k;
cin >> n >> k;
vector<ll> a(n), b(n);
for (ll &x : a) cin >> x;
for (ll &x : b) cin >> x;
vector<pair<ll, ll>> seg(n);
ll ans = 0;
for (int i = 0; i < n; i++) {
ll l = min(a[i], b[i]);
ll r = max(a[i], b[i]);
seg[i] = {l, r};
ans += r - l;
}
sort(seg.begin(), seg.end());
ll bestGap = LLONG_MAX;
ll mxRight = seg[0].second;
for (int i = 1; i < n; i++) {
ll l = seg[i].first;
ll r = seg[i].second;
if (l <= mxRight) {
bestGap = 0;
break;
}
bestGap = min(bestGap, l - mxRight);
mxRight = max(mxRight, r);
}
cout << ans + 2 * bestGap << '\n';
}
}