Source Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;


using ll = long long;
using ull = unsigned long long;
using ld = long double;

template<class __set_element_type>
using gnu_set= tree<__set_element_type, null_type,less<__set_element_type>, rb_tree_tag,tree_order_statistics_node_update>;
template<class __key_type, class __val_type>
using gnu_map = tree<__key_type, __val_type,less<__key_type>, rb_tree_tag,tree_order_statistics_node_update>;

#define ios ios::sync_with_stdio(0); cin.tie(0)


const ll oo = 1e18;
const ll MOD = 1e9 + 7;

int t, n, k;

void solve() {
    cin >> n >> k;
    vector<ll> vec(n), prefix(n), suffix(n);
    for(auto &v: vec)
        cin >> v;
    for(int i = 0; i < n; i++) 
        prefix[i] = vec[i] + (i > 0 ? prefix[i-1] : 0);
    for(int i = n-1; i >= 0; i--) 
        suffix[i] = vec[i] + (i < n - 1 ? suffix[i+1] : 0);
    
    ll result = 0;
    for(int x = 0; x <= n; x++) {
        ll y = x ^ k;
        if(x + y > n)
            continue;
        ll local = (x > 0 ? prefix[x-1] : 0) + (y > 0 ? suffix[n - y] : 0);
        result = max(result, local);  
    }
    cout << result << '\n';
}

int main() {
    ios;
    cin >> t;
    while(t--)
        solve();
}
Copy
Midterms Ammar Abu Yaman
GNU G++17
34 ms
5.1 MB
Accepted