Novice D
Alice is able to determine what move Bob makes as Bob can only make one move to maximize the results.
Since Alice can only change one element of $$$A$$$, for all the unchanged elements we can precompute how much the result would increase by if Bob selects that index. We want the maximum between the greatest possible change of all those other indices and Bob picking $$$j = i$$$ after Alice's operation.
Next, we want the minimum over all $$$i$$$. We can do this by finding the two $$$j$$$ with the largest value of $$$A_j | (y - A_j)$$$. We know that the maximum of all the indices aside from $$$i$$$ will be one of these two. Thus, when checking $$$i$$$, if the $$$j$$$ that maximizes (A_j \mid (y — A_j)) equals (i), then we take the second largest. We solve the problem in $$$O(n)$$$.
include <bits/stdc++.h>
using namespace std; using ll = long long;
int main(){ cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while(t--){
int n; ll x, y;
cin >> n >> x >> y;
vector<ll> vals(n);
ll sum = 0;
for(int i = 0; i < n; ++i) {
cin >> vals[i];
sum += vals[i];
}
vector<ll> loss(n);
for(int i = 0; i < n; ++i) loss[i] = vals[i]-(vals[i]&y);
ll min1 = LLONG_MAX, min2 = LLONG_MAX;
int idx1 = -1;
for(int i = 0; i < n; ++i) {
if(loss[i] < min1){
min2 = min1;
min1 = loss[i];
idx1 = i;
} else if(loss[i] < min2) {
min2 = loss[i];
}
}
ll ans = LLONG_MAX;
for(int i = 0; i < n; ++i) {
ll alice = vals[i]|x;
ll delta = alice-vals[i], same = alice-(alice&y);
ll obest = ((i == idx1) ? min2 : min1);
ll bob = min(obest, same);
ll temp = sum+delta-bob;
ans = min(ans, temp);
}
cout << ans << "\n";
}
return 0;}



