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.
Prefix sign flips commute, and only whether each prefix is flipped an odd or even number of times matters. Start by sorting the post positions.
Let the sorted positions be . They split the array into blocks , plus the suffix after . Every element inside one block always receives exactly the same flips, so that block contributes either its sum or the negation of its sum.
The block signs look coupled because the prefixes are nested, but process the post positions from right to left. When considering , using that post toggles block while touching none of the already-fixed blocks .
Hence every combination of signs for the first blocks is achievable. Choose each block's sign so its contribution is nonnegative, giving . The suffix after the largest post cannot be flipped at all, so its sum must be added without an absolute value.
With and prefix sums , the answer is Compute , sort the post positions, and evaluate this in time.
The important move is to stop tracking individual operations on individual elements. The post positions are the only boundaries where two neighboring elements can behave differently.
Sort the post positions:
and define . These positions divide the array into the following blocks:
followed by the suffix
Let the sum of block be .
Consider two elements in the same block . There is no post position between them, so every chosen prefix either contains both elements or contains neither. They therefore receive exactly the same number of sign changes. The final contribution of the whole block is consequently either
or
The suffix is different: every available prefix ends at or before , so this suffix is never changed. Its contribution is permanently , even if that number is horribly negative. No magical absolute value there.
A post at flips exactly the blocks
At first this looks annoying: one operation changes several block signs. However, any desired signs for can be constructed by processing blocks from right to left.
Start with . Among operations not yet decided, only the post at can change its sign, so use that post exactly when necessary to give its desired sign.
Next handle . The earlier decision at may already have flipped it, but the post at lets us either keep or toggle its current sign. Crucially, this post ends before , so it cannot ruin the sign already fixed for .
Continue in the same way down to . Thus every block sign can be chosen independently.
The same fact can be written algebraically. If says whether the post at is read, then the final sign of is
Given any desired , set and choose so that
So there really is no hidden compatibility condition. Nested prefixes were just wearing a fake mustache.
For every flippable block , its contribution can only be or . Neither can exceed , and because all block signs are independently achievable, we can attain for every simultaneously.
Therefore,
This gives both sides of the proof:
Hence the formula is optimal.
Build prefix sums
Then
for , while
The final formula is
For the first sample, the sorted post positions are . The corresponding block sums are , and there is no remaining suffix. Their absolute values sum to .
For the third sample, the only flippable block has sum , while the untouchable suffix has sum . The answer is therefore . This is exactly why taking the absolute value of the suffix would be bogus.
Lemma 1. Each block for contributes either or , while always contributes .
Proof. All elements of belong to exactly the same available prefixes, so every chosen operation flips all of them together. No available prefix contains any element after . Therefore the stated contributions follow.
Lemma 2. Every assignment of signs to is achievable.
Proof. Process . At step , choose whether to use the post at so that gets its desired sign. This operation does not touch any block with , and later operations have even smaller endpoints, so no already-fixed sign changes. All desired signs are obtained.
Theorem. The algorithm outputs the maximum possible total productivity.
Proof. By Lemma 1, each with contributes at most , and the suffix contributes exactly . Thus no result exceeds . By Lemma 2, we can choose every flippable block's sign so that it contributes , attaining this bound. The algorithm computes precisely that value.
Sorting costs , and building and scanning the prefix sums costs . The total time is per test case, with memory.
Use long long: sums can have magnitude around , which absolutely does not fit in a 32-bit int.
#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, m;
cin >> n >> m;
vector<ll> pref(n + 1);
for (int i = 1; i <= n; ++i) {
ll x;
cin >> x;
pref[i] = pref[i - 1] + x;
}
vector<int> b(m);
for (int &x : b) cin >> x;
sort(b.begin(), b.end());
ll answer = 0;
int previous = 0;
for (int endpoint : b) {
answer += abs(pref[endpoint] - pref[previous]);
previous = endpoint;
}
answer += pref[n] - pref[previous];
cout << answer << '\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, m;
cin >> n >> m;
vector<ll> pref(n + 1);
for (int i = 1; i <= n; ++i) {
ll x;
cin >> x;
pref[i] = pref[i - 1] + x;
}
vector<int> b(m);
for (int &x : b) cin >> x;
sort(b.begin(), b.end());
ll answer = 0;
int previous = 0;
for (int endpoint : b) {
answer += abs(pref[endpoint] - pref[previous]);
previous = endpoint;
}
answer += pref[n] - pref[previous];
cout << answer << '\n';
}
return 0;
}