Source Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define sp ' '
#define assert(x,a) if(x){cout << a << endl;return;}
#define sortv(x)sort(x.begin(),x.end())
#define revev(x)reverse(x.begin(),x.end())
inline void fast(){
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
    }
vector<vector<int>>graph;

vector<bool>vis(100001,false);
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
inline void answer(int c){assert(c==0,"NO")cout << "YES" << endl;}
void solve()
{
        int n, k; cin >> n >> k;
        vector<ll>l(n); for (ll& e : l)cin >> e;
        vector<ll>prefix_sum(n + 2);
        vector<ll>suffix_sum(n + 2);
        prefix_sum[0] = 0;
        prefix_sum[1] = l[0];
        for (int i = 2; i <= l.size(); i++)
        {
            prefix_sum[i] = prefix_sum[i - 1] + l[i - 1];
        }
        reverse(l.begin(), l.end());
        suffix_sum[0] = 0;
        suffix_sum[1] = l[0];
        for (int i = 2; i <= l.size(); i++)
        {
            suffix_sum[i] = suffix_sum[i - 1] + l[i - 1];
        }
        int test = l.size();
        ll _make = 0;
        while (test != 0)
        {
            ll last1 = test;
            ll last2 = test ^ k;
            if(last1 + last2 <= n)
             _make = max(_make, max(suffix_sum[last1] + prefix_sum[last2], suffix_sum[last2] + prefix_sum[last1]));
            test--;
        }
        cout << _make << endl;
}
int main()
{
    fast();
    ll t{ 1 };cin >> t;
  // cin.ignore();
    while (t--)
    {
        solve();
    }
}
Copy
Midterms YousefShaheen
GNU G++17
35 ms
5.1 MB
Accepted