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);
   cin(v);
   int l=0 , r = n-1;
   int cntl = 0,cntr = 0;
   ll sum = 0;
   ll ans = 0;
   while(l<=r)
   {
	  if(v[l]>v[r])
	  {
		 sum+=v[l++];
		 cntl++;
	  }
	  else 
	  {
		  sum+=v[r--];
		  cntr++;
	  }
	  if((cntl^cntr) == k)
	   ans = max(ans,sum);
   }
   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
31 ms
556 KB
Wrong Answer