Skip to content

Commit 91aeb25

Browse files
authored
Create 2019-G-B.cpp
1 parent f3469a7 commit 91aeb25

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

kick-start/2019-G-B.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<bits/stdc++.h>
2+
#define ms(x,v) memset((x),v,sizeof(x))
3+
#define INF 0x3f3f3f3f
4+
using namespace std;
5+
6+
const int MAXN = 100+10;
7+
typedef long long LL;
8+
9+
10+
void solve(){
11+
LL N,M;
12+
cin >> N >> M;
13+
LL ans =0;
14+
LL sum[64];
15+
ms(sum,0);
16+
const int MAX_BIT = 52;
17+
for(int i=0 ;i< N ; ++i){
18+
LL x;
19+
cin >> x;
20+
for(int j =0;j<MAX_BIT ; ++j)
21+
if((x >>j) & 1)sum[j] ++;
22+
23+
}
24+
vector<LL> dp(MAX_BIT+1,0);
25+
dp[0] = 0;
26+
for(int i=0; i<MAX_BIT ; ++i){
27+
dp[i+1] = dp[i] + min(sum[i],N-sum[i]) * (1LL << i);
28+
}
29+
for(int j = MAX_BIT -1 ; j>=0 ; --j){
30+
LL val = (N-sum[j]) * (1LL << j);
31+
if(dp[j] <= M - val){
32+
ans |= (1LL << j);
33+
M-=val;
34+
}else{
35+
M-=sum[j] * (1LL << j);
36+
}
37+
if( M < dp[j]){
38+
ans = -1;
39+
break;
40+
}
41+
}
42+
cout << ans << "\n";
43+
}
44+
45+
46+
47+
48+
int main(){
49+
ios :: sync_with_stdio(0);
50+
cin.tie(0);
51+
// cout.tie(0);
52+
std::cout.precision(10);
53+
std::cout.setf( std::ios::fixed, std:: ios::floatfield );
54+
55+
int T;
56+
cin >> T;
57+
for(int tt =1 ; tt <=T ; ++tt)
58+
{
59+
cout << "Case #" << tt << ": ";
60+
solve();
61+
}
62+
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)