First i increased all values by xor and if all values increases and return sum else let’s say c values increase for rest k-c values i calculated k-c min values of a[i]-a[i]^x and compare this with previous non updated array to get maxsum

Here’s my code

#include<bits/stdc++.h>

using namespace std;

int main()

{ long long int t,n,x,k,ans=0,c=0,m=0,sum=0,su=0,bi=0;

cin>>t;

while(t–)

{

cin>>n;

long long int a[n],b[n],point=1;

for(int i=0;i<n;i++)

{

cin>>a[i];

b[i]=a[i];

su+=a[i];

}

cin>>k>>x;

```
//loop to uodate all values which can be increased by xor
```

while(point==1)

{ c=0;

for (int i = 0; i < n; i++)

{ m=a[i]^x ;

if(m>a[i])

{ a[i]=m;

c++;

}

if(c==k)

{

point = 1;

for(int j=0;j<=i;j++)

if(b[j]!=a[j])

b[j]=a[j];

break;

}

}

if(c<k)

point =0;

}

// c=0 means all elements have increased their value

if(c==0)

{for(int i=0;i<n;i++)

{

ans+=a[i];}}

else

{ // calculate min of a[i]-a[i]^x of all elements

long long int diff=k-c,index=0,count=0;

```
vector<pair <long long int,long long int>> v;
for(int i=0;i<n;i++)
if(a[i]==b[i])
{ m=a[i]^x;
v.push_back(make_pair(a[i]-(m),i));
index++;
ans+=(b[i]);
}
else
ans+=b[i];
sort(v.begin(),v.end());
vector <int > vec; // stores index of elements with min a[i]-a[i]^x
for(int i=0;i<diff;i++)
{ vec.push_back(v[i].second);
long long int u=a[vec[i]];
m=u^x;
sum+=(m);
}
sort(vec.begin(),vec.end());
count=0;
for(int i=0;i<n;i++)
{
if(count<diff)
{ if(i==vec[count])
count++;}
else
sum+=a[i];
}
// calculate max of updated and non updated array sum
ans=max(ans,sum) ;
```

}

ans=max(su,ans);

cout<<ans<<"\n";

ans=0,sum=0,point=1,su=0,bi=0;

}

return 0;

}