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.
First isolate the part Alice cannot avoid. Any valid walk from index to index must visit every index at least once, so the straight walk contributes
If , Alice is dead no matter how Bob arranges the array. All values are nonnegative, so extra moves can only keep or increase the sum, never decrease it.
Every useless detour is basically walking across some adjacent edge and coming back. For edge , that adds
to the total.
So Alice can reach exactly sums of the form
The only way to add is having a neighboring and .
Bob can block only the target : arrange the values as all s, then all s, then all s. This avoids adjacent pairs. For every target at least , Alice can use additions and , so Bob is cooked.
Let
This is the sum Alice gets from the straight path
On a line, any walk from to must visit every index at least once. Since every , Alice can never get a sum smaller than .
So if , Bob can output any rearrangement. Alice cannot go below the mandatory sum.
If , Bob cannot stop Alice, because the straight path already works.
What extra movement does
Suppose Alice is at index and wastes time by going
That detour adds
Similarly, going left and then right across the same edge adds the same value. Therefore, after the mandatory straight path, every extra contribution is a nonnegative combination of adjacent pair sums:
Conversely, Alice can realize any such combination by doing the needed back-and-forth detours before continuing to the right. So this characterization is exact, not just hand-wavy vibes.
Now values are only , so adjacent pair sums can be:
The important one is . It happens only when a is adjacent to a .
If some adjacent pair has sum , Alice can add as many times as she wants. Then every target is reachable. Bob definitely does not want that.
So to block , Bob should avoid every adjacent pair. A clean arrangement is:
Its adjacent pair sums are only:
There is no way to add exactly . So if , this arrangement works for Bob.
Why larger targets cannot be blocked
Take any arrangement.
If it has a neighboring and , Alice can add , so all sums are reachable.
Otherwise, there is no adjacent pair. Since the array contains at least one , one , and one , any path in the array from a position to a position must pass through s. That means somewhere there is a next to a , and somewhere there is a next to a .
So Alice can add both:
and
Using and , Alice can make every integer at least :
So every target with is always reachable. Bob cannot block those.
Final cases:
That is the whole trick. No DP needed; DP would be using a sledgehammer on a gummy bear.
#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, s;
cin >> n >> s;
vector<int> cnt(3, 0);
int sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
sum += x;
}
if (s < sum) {
for (int v = 0; v <= 2; v++) {
while (cnt[v]--) cout << v << ' ';
}
cout << '\n';
} else if (s == sum + 1) {
while (cnt[0]--) cout << 0 << ' ';
while (cnt[2]--) cout << 2 << ' ';
while (cnt[1]--) cout << 1 << ' ';
cout << '\n';
} else {
cout << -1 << '\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, s;
cin >> n >> s;
vector<int> cnt(3, 0);
int sum = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
sum += x;
}
if (s < sum) {
for (int v = 0; v <= 2; v++) {
while (cnt[v]--) cout << v << ' ';
}
cout << '\n';
} else if (s == sum + 1) {
while (cnt[0]--) cout << 0 << ' ';
while (cnt[2]--) cout << 2 << ' ';
while (cnt[1]--) cout << 1 << ' ';
cout << '\n';
} else {
cout << -1 << '\n';
}
}
return 0;
}