Problem:
Score : 500 points

Problem Statement

Seisu-ya, a store specializing in non-negative integers, sells N non-negative integers. The i-th integer is A_i and has a utility of B_i.
There may be multiple equal integers with different utilities.

Takahashi will buy some integers in this store. He can buy a combination of integers whose bitwise OR is less than or equal to K. He wants the sum of utilities of purchased integers to be as large as possible.

Find the maximum possible sum of utilities of purchased integers.

Constraints

1 \leq N \leq 10^5

0 \leq K < 2^{30}

0 \leq A_i < 2^{30}(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N K
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible sum of utilities of purchased integers.

Sample Input 1

3 5
3 3
4 4
2 5

Sample Output 1

8

Buy 2 and 3 to achieve the maximum possible total utility, 8.

Sample Input 2

3 6
3 3
4 4
2 5

Sample Output 2

9

Buy 2 and 4 to achieve the maximum possible total utility, 9.

Sample Input 3

7 14
10 5
7 4
11 4
9 8
3 6
6 2
8 9

Sample Output 3

32

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>

#define INF 1000010000
#define nl '\n'
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))
#define sz(c) (c).size()
#define rep(i,n) for( int i = 0; i < n; ++i )
#define repi(i,n) for( int i = 1 ; i <= n; ++i )
#define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
#define repf(j,i,n) for( int j = i ; j < n ; ++j )
#define die(s) {std::cout << s << nl;}
#define dier(s) {std::cout << s; return 0;}
#define vi vector<int>
typedef long long ll;
#define MAXN 101010
using namespace std;


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.precision(0); 

	int n , k;
	cin >> n >> k;
	
	vector<pii> v(n);
	ll ans = 0;
	ll ans1 = 0;
	rep(i , n){
		cin >> v[i].fi >> v[i].se;
		repn(j , 32){
			if((k & (1 << j)) && (v[i].fi & (1 << j))){
				break;
			}else if(k & (1 << j)){
				ans1 += v[i].se;
			}else if(v[i].fi & (1 << j)){
				break;
			}
		}
	}
	
	int p = -1;
	repn(i , 32){
		if(k & (1 << i)){
			p = i;
			break;
		}
	}
	
	if(p == -1){
		ll ans = 0;
		rep(i , n){
			if(v[i].fi == 0)ans += v[i].se;
		}
		dier(ans);
	}
	//cout << "p=" << p << nl;
	repn(i , p){
		if(k & (1 << i)){
			ll cur = 0;
			rep(j , n){
				int x = v[j].fi;
				bool ok = 1;
				repn(s , 32){
					if(s == i)break;
					if((x & (1 << s)) && !(k & (1 << s))){
						ok = 0;
						break;
					}
					
				}
				ok &= !(x & (1 << i));
		//		cout << i << " " << x << " " << ok << nl;
				if(!ok)continue;
				cur += v[j].se;
			}
		//	cout << nl;
			ans = max(ans , cur);
		}else{
			ll cur = 0;
			rep(j , n){
				int x = v[j].fi;
				bool ok = 1;
				repn(s , 32){
					if((x & (1 << s)) && !(k & (1 << s))){
						ok = 0;
						break;
					}
				}
				if(!ok)continue;
				cur += v[j].se;
			}
			ans = max(ans , cur);
		}
		
	}
		

	cout << max(ans , ans1) << nl;

	return 0;
}