Source Code
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define ll long long
#define pb push_back
#define cin(x) for(auto &i:x)cin>>i;
#define cout(x) for(auto i:x)cout<<i<<" ";
#define copy(x,y) for(int i = 0 ; i < (int)x.size();i++)x[i]=y[i];
#define imn INT_MIN
#define imx INT_MAX
#define llmn LONG_LONG_MIN
#define llmx LONG_LONG_MAX
using namespace std;
const ll MOD = 1000000007;
const double PI = 3.14159265358979323846;
void solve()
{
   int n,k;
   cin>>n>>k;
   vector<int> v(n+1);
   for (int i = 1; i <= n; i++)
   {
	   cin>>v[i];
   }
   vector<int> pre(n+1,0);
   for (int i = 1; i <= n; i++)
   {
	   pre[i] = pre[i-1] + v[i];
   }
   vector<int> suf(n+1,0);
   reverse(all(v));
   for (int i = 1; i <= n; i++)
   {
	   suf[i] += suf[max(i-1,0)] + v[max(i-1,0)];
   }
   ll ans = 0;
   for (int i = 1; i <= n; i++)
   {
	   if(i+(i^k)>n)continue;
	   ans = max({ans,(ll)pre[i]+suf[max((i^k),0)],(ll)pre[max((i^k),0)]+suf[i]});
   }
   cout<<ans<<endl;
}
int main()
{
	FAST;
	//freopen("Round1.in","r",stdin);
	//freopen("overcode.in","w",stdout);
	int t = 1;
	cin >> t;
	while (t--)
      solve();
}
Copy
Midterms moathhamudah
GNU G++17
36 ms
3.3 MB
Accepted