Language: C++

#include <iostream>  // cin,cout
#include <stdio.h>   // printf,scanf (standard input/output)
#include <cmath>     // abs(x),pow(x,2)
#include <string>
#include <string.h>  // strlen, strcmp
#include <algorithm> // min,max, sort(v.begin(),v.end())
#include <vector>
#include <time.h>
using namespace std;


long long Func(int N, int x, int list[]){
    long long result = 0;
    for (int i=0;i<=x;i++) {
        result += pow(2,i)*max(list[i],N-list[i]);
    }
    return result;
}

int Func2(long long x) {
    int result = 39;
    
    if (x==0) return -1;
    
    while (true) {
        if (x-pow(2,result)>=0) {
            return result;
        }
        result -= 1;
    }
}

int main(){
    long long N;
    long long K;
    long long A[100000];
    
    cin >> N >> K;
    for (int i=0;i<N;i++) cin >> A[i];
    
    long long list[40] = {};
    
    for (int i=0;i<N;i++) {
        long long temp = A[i];
        int j = 0;
        while (temp!=0) {
            long pow2 = pow(2,39-j);
            list[39-j] += temp/pow2;
            temp = temp%pow2;
            j += 1;
        }
    }

    
//    long long aaa = 0;
//    int a = 39;
//    while (a>=0) {
//        if (K>=pow(2,a)) {
//            K -= pow(2,a);
//            aaa += pow(2,a)*(N-list[a]);
//        }
//        else {
//            aaa += pow(2,a)*list[a];
//        }
//        a -= 1;
//    }
//    cout << aaa << endl;
//    return 0;
    
    
    if (K==0) {
        long long result_0 = 0;
        for (int i=0;i<40;i++) {
            result_0 += pow(2,i)*list[i];
        }
        cout << result_0 << endl;
    }

    else {
        vector<long long> result;
        long long now = 0;
        long long prev_x = 39;
        while (true) {
            int x = Func2(K);
            if (x>=0) {
                for (int i=prev_x;i>x;i--) now += pow(2,i)*list[i];
                result.push_back(now + pow(2,x)*list[x] + Func(N, x-1, list));
                now += pow(2,x)*(N-list[x]);
                K -= pow(2,x);
                prev_x = x-1;
            }
            else{
                long long temp = 0;
                for (int i=0;i<=prev_x;i++) temp += pow(2,i)*list[i];
                result.push_back(now + temp);
                //result.push_back(result[result.size()-1] - pow(2,prev_x+1)*list[prev_x+1] - Func(N, prev_x, list) + pow(2,prev_x+1)*(N-list[prev_x+1]) + temp);
                break;
            }
        }
        //for (int i=0;i<result.size();i++) cout << result[i] << endl;
        cout << *max_element(result.begin(),result.end()) << endl;
    }
    
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:87:72: error: cannot convert 'long long int*' to 'int*'
   87 |                 result.push_back(now + pow(2,x)*list[x] + Func(N, x-1, list));
      |                                                                        ^~~~
      |                                                                        |
      |                                                                        long long int*
a.cc:12:34: note:   initializing argument 3 of 'long long int Func(int, int, int*)'
   12 | long long Func(int N, int x, int list[]){
      |                              ~~~~^~~~~~