#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;
    }
}
//#include <fstream>   // ifstream, ofstream
//#include <sstream>   // istringstream

int main(){
    int N = 100;
    long long K;
    long long A[100010];
    
//    string filename = "./text.txt";
//
//    ifstream reading_file;
//    reading_file.open(filename, std::ios::in);
//
//    string reading_line_buffer;
//
//    //cout << "reading " << filename << "..." << std::endl;
//    int i=0;
//    while (i!=N+2)
//    {
//        // read by line
//        getline(reading_file, reading_line_buffer);
//
//        cout << reading_line_buffer << endl;
//        if (i==0) N = stoi(reading_line_buffer);
//        else if (i==1) K = stoll(reading_line_buffer);
//        else A[i-2] = stoll(reading_line_buffer);
//
//        i++;
//    }
    
    cin >> N >> K;
    for (int i=0;i<N;i++) cin >> A[i];
    
    int list[40] = {};
    
    for (int i=0;i<N;i++) {
        long long temp = A[i];
        int j = 0;
        while (temp!=0) {
            long long pow2 = pow(2,39-j);
            list[39-j] += temp/pow2;
            temp = temp%pow2;
            j += 1;
        }
    }
//    for (int i=0;i<40;i++) cout << list[i] << " ";
//    cout << endl;
    
    
    if (K==0) {
        long long result_0 = 0;
        for (int i=0;i<40;i++) {
            result_0 += (long long)pow(2,i)*list[i];
        }
        cout << result_0 << endl;
    }

    else {
        vector<long long> result;
        long long now = 0;
        int prev_x = 39;
        while (true) {
            int x = Func2(K);
            if (x>=0) {
                for (int i=prev_x;i>x;i--) now += (long long)pow(2,i)*list[i];
                result.push_back(now + (long long)pow(2,x)*list[x] + Func(N, x-1, list));
                //cout << x << " " << now << " " << pow(2,x)*list[x] << " " << Func(N, x-1, list) << endl;
                now += (long long)pow(2,x)*(N-list[x]);
                K -= (long long)pow(2,x);
                prev_x = x-1;
            }
            else{
                long long temp = 0;
                for (int i=0;i<=prev_x;i++) temp += (long long)pow(2,i)*list[i];
                result.push_back(now + temp);
                //cout << now + temp << endl;
                //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;
//        long long ans = 0;
//        for (int i=0;i<result.size();i++) ans = max(ans,result[i]);
//        cout << ans << endl;
    }
    
    return 0;
}

Measured memory_bytes: 1024