Getting WA in MAXAND18

Question: MAXAND18 Problem - CodeChef

My code: CodeChef: Practical coding for everyone :

#include <bits/stdc++.h>
#define ll long long
using namespace std;

bool sortfun(pair<ll,ll> p1, pair<ll,ll> p2)
{
if (p1.second>p2.second) return true;
if (p1.second==p2.second && p1.first>p2.first) return false;
return false;
}

int main() {
ll t;
cin>>t;
while (t–)
{
ll n,k;
cin>>n>>k;
ll a[n],max=0;
for (ll i=0;i<n;i++)
{
cin>>a[i];
max=max>(log2(a[i])+1)?max:(log2(a[i])+1);
}
pair <ll,ll> p[max];
for (ll i=0;i<max;i++)
p[i].second=0;
for (ll i=max-1;i>=0;i–)
{
p[i].first=i;
for (ll j=0;j<n;j++)
{
ll an=((1<<i)&a[j]);
if (an!=0)
p[i].second+=(ll)pow(2,i);
}
}
sort (p,p+max,sortfun);
ll out=0;
for (ll i=0;i<k && i<max;i++)
{
out+=(ll)pow(2,p[i].first);
}
cout<<out<<endl;
}
return 0;
}

Please let me know what the mistake in my code is.
@dean_student

Thank you!

First Thing never trust log and pow functions.
You can use << >> for those dealing with power 2.
You can read editorial it is there for a purpose.

https://www.codechef.com/viewsolution/34886979
refer this might help , so, we need to find the max(( position*number of bits)…k)

1 Like