Language: C++

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

const int MOD = (int)1e9+7;

int main(){
  long long N,K;
  cin >> N >> K;
  vector<long long> A(N);
  for(int i=0;i<N;i++)  cin >> A.at(i);

  vector<int> k(50);
  int cnt=0;
  while(K>0){
    if(K%2==1)  k.at(cnt)++;
    K/=2;
    cnt++;
  }
  cnt--;
  vector<int> bitA(50);
  for(int i=0;i<N;i++){
    long long x=A.at(i);
    int count=0;
    while(x>0){
      if(x%2==1)  bitA.at(count)++;
      x/=2;
      count++;
    }
  }

  long long ans=0;
  for(int j=cnt;j>=-1;j--){
    vector<int> y(50);
    for(int s=0;s<50;s++){
      if(j==s) y.at(s)=0;
      else if(j>s)  y.at(s)=k.at(s);
      else if(j<s)  y.at(s)=-1;
    }

    long long ansx=0;
    long long count2=1;
    for(int i=0;i<bitA.size();i++){
      long long mx;
      if(y.at(i)==0) mx=bitA.at(i);
      else if(y.at(i)==1) mx=N-bitA.at(i);
      else if(y.at(i)==-1)  mx=max(bitA.at(i),N-bitA.at(i));
      ansx+=count2*mx;
      count2*=2;
    }
    ans=max(ans,ansx);
  }

  cout << ans << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:46:35: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, long long int)'
   46 |       else if(y.at(i)==-1)  mx=max(bitA.at(i),N-bitA.at(i));
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:46:35: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   46 |       else if(y.at(i)==-1)  mx=max(bitA.at(i),N-bitA.at(i));
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:46:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   46 |       else if(y.at(i)==-1)  mx=max(bitA.at(i),N-bitA.at(i));
      |                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~