Source Code
#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds; // pb_ds
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>


using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define EPS (1e-9)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define debug(x) cerr << #x << ": " << (x) << endl;

void _IO_() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#ifdef ELARABY
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
}

//             N  E  S  W  NW  NE SE SW
//int dr[] = {-1, 0, 1, 0, -1, -1, 1, 1};
//int dc[] = {0, 1, 0, -1, -1, 1, 1, -1};


template<typename T>
void input(vector<T> &v) {
    for (T &i: v) {
        cin >> i;
    }
}

template<typename T>
void output(const vector<T> &v, const int inc = 0) {
    for (int i = 0; i < int(v.size()); ++i) {
        if (i) cout << ' ';
        cout << v[i] + inc;
    }
    cout << '\n';
}

template<typename T>
void output(const vector<pair<T, T>> &v, const int inc1 = 0, const int inc2 = 0) {
    for (int i = 0; i < int(v.size()); ++i) {
        cout << v[i].first + inc1 << ' ' << v[i].second + inc2 << '\n';
    }
}
//////////////////------PROBLEM SOLUTIONS START FROM HERE------///////////////////



int main() {
    _IO_();
//    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

    int tt;
    cin >> tt;
    while (tt--) {
        int n, k;
        cin >> n >> k;

        vector<int> a(n + 2);
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
        }
        vector<ll> pref(n + 2);
        for (int i = 1; i <= n; ++i) {
            pref[i] = pref[i - 1] + a[i];
        }
        vector<int> suf(n + 2);
        for (int i = n; i >= 1; --i) {
            suf[i] = suf[i + 1] + a[i];
        }

        ll ans = 0;
        for (int x = 0; x <= n; ++x) {
            const int y = k ^ x;
            const int b = n + 1 - y;
            const int a = x;
            if (b <= a || y > n) continue;
            ans = max(ans, pref[a] + suf[b]);
        }
        cout << ans << '\n';
    }

    return 0;
}
Copy
Midterms Omar_Elaraby
GNU G++17
30 ms
3.4 MB
Accepted