Source Code
#include<iostream>
#include<algorithm>
#include<deque>
#include<cmath>
#include<vector>
#include<string>
#include<bitset>
#define OmarNasr {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
using namespace std;
long long p[200002], s[200002];
int main()
{
	long long t, n, k;
	cin >> t;

	while (t--)
	{
	    long long mx = 0;

		cin >> n >> k;
		for (int i = 1; i <= n; i++)
		{
			cin >> p[i];
			p[i] += p[i - 1];
		}
		int c = 1;
		s[n] = p[n];
		for (int i = n - 1; i >= 1; i--)
			s[c++] = p[n] - p[i];

		for (int i = 0; i <= n; i++)
		{
			for (int j = 0; j <= n; j++)
			{
				if (1ll*i + j <= n)
				{
					if ((p[i] + s[j]) > mx && (i ^ j) == k)
						mx = (p[i] + s[j]);
				}
				else break;

			}
		}

		cout << mx << "\n";
	}
}
Copy
Midterms OmarNasr
GNU G++17
2072 ms
3.4 MB
Time Limit Exceeded