Skip to content

Instantly share code, notes, and snippets.

@karaketir16
Created March 15, 2020 21:54
Show Gist options
  • Save karaketir16/8eeba7289b764ee6786a111352ae1701 to your computer and use it in GitHub Desktop.
Save karaketir16/8eeba7289b764ee6786a111352ae1701 to your computer and use it in GitHub Desktop.
#include "bits/stdc++.h"
using namespace std;
typedef __int128_t lint;
typedef __uint128_t ulint;
#define mod(x) x%1000000007
long long int n,mm;
lint tot = 0;
lint bits[64];
#define IsBitSet(num,bit) ((num >> bit) &1ULL)
std::ostream&
operator<<( std::ostream& dest, __int128_t value )
{
std::ostream::sentry s( dest );
if ( s ) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[ 128 ];
char* d = std::end( buffer );
do
{
-- d;
*d = "0123456789"[ tmp % 10 ];
tmp /= 10;
} while ( tmp != 0 );
if ( value < 0 ) {
-- d;
*d = '-';
}
int len = std::end( buffer ) - d;
if ( dest.rdbuf()->sputn( d, len ) != len ) {
dest.setstate( std::ios_base::badbit );
}
}
return dest;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >>mm;
lint m = mm;
unsigned long long int x = 0;
for(int i =0; i<n; i++)
{
cin >> x;
for(int j =0; j< 64; j++)
{
bits[j] += IsBitSet(x,j);
}
tot += x;
}
unsigned long long int res = 0;
for(int j = 0; j < 64; j++)
{
bits[j] = (n - bits[j]) - bits[j];
// cout <<"j: "<<j << " bits j: " <<bits[j] << endl;;
if(bits[j] <= 0)
{
res += (((unsigned long long int)1)<<j);
tot += bits[j]*(((lint)1)<<j);
}
}
if(tot > m)
{
cout << -1;
return 0;
}
// cout << res << endl;
for(lint j =63; j >= 0; j--)
{
if(bits[j] <= 0) continue;
// cout << "j: "<<j << endl;
// cout << "tot: " << tot << endl;
// cout << "bits[j]*(1<<j): " << bits[j]*(((lint)1)<<j) << endl;
if(tot + bits[j]*(((lint)1)<<j) <= (lint) m)
{
tot += bits[j]*(((lint)1)<<j);
res += (((unsigned long long int)1)<<j);
}
}
cout << res;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment