June long challenge lent money help getting wa

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;
}

We create a new array, where we store the effect we have on a number when we xor it with ‘x’ :slight_smile: (b[i]= a[i]-a[i]^x) . Xor any number with ‘x’ 2 times/even number of times and you get the same result :slight_smile: …a[i]^x^x…(even number of times)=a[i].
if k is odd, answer is the sum of all positive numbers;
if k is even and there are even number of positive numbers in the array; answer is same as above.
if k is even,and number of positive numbers is odd, then answer is max(positive sum-smallest positive number,positive_sum+smallest negative number ). Hope this helps :slight_smile:

The question can be solved like this:
If k is not equal to n then you can make exaclty two different.Consider a case where you change the elements from 1 to k and next from 2 to k+1.Now after this operation you can see than only two elements are changed those are 1 and k+1 (You can modify the query in such a way that any two are converted).
After this consider that k is odd.Now make a change from 1 to k.Now convert two again again and fillany you would be left with only one element that is changed.
So you can infer that that if k is odd then minimum you can change is 1 and if k is even then minimum you could change is 2.
After this you can sort then values based on new-old and then take them accordingly.
If k is equal to n then either you change all or change none no speciall cases

What’s wrong in my approach?