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: Runtime Error)

#include "bits/stdc++.h"
#ifndef err
#define err(...)(void)0
#endif
using namespace std;
using ll = long long;
using ull = decltype(1ull);
template<class T>int size(T&&a) { return a.size(); }
#define REP(t,a)for(typename make_signed<decltype(a.second)>::type t=a.first,_l##t=a.second;t<_l##t;t++)
#define RREP(t,a)for(typename make_signed<decltype(a.second)>::type t=a.second-1,_l##t=a.first;t>=_l##t;t--)
template<class B>pair<int, B>make_pair(B b) { return{ 0,b }; }
#define rep(t,...)REP(t,make_pair(__VA_ARGS__))
#define rrep(t,...)RREP(t,make_pair(__VA_ARGS__))
#define all(a)begin(a),end(a)
#define rall(a)a.rbegin(),a.rend()
#define var(t,a, ...) t a,##__VA_ARGS__;auto a##_in = [&]() { return in, a, ##__VA_ARGS__, 0; }();
#define In(a, ...) auto k##a##_in = [&]() { __VA_ARGS__; return 0; }();

void Calc();
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);
  cin.exceptions(istream::failbit | istream::badbit);
  cout << fixed << setprecision(15); Calc(); cout.flush(); return 0;
}
template<class A>void amax(A&a, A b) { a = max(a, b); }
template<class A>void amin(A&a, A b) { a = min(a, b); }
template<class A>int comp(A a, A b) { return a < b ? 1 : b < a ? -1 : 0; }
struct Scanner {
  template<class A = string>A Next() { A a; cin >> a; return a; }
  template<class A = int>vector<A>Array(int n) { vector<A>a(n); for (A&i : a) cin >> i; return a; }
  string Line() const { string s; getline(cin, s); return s; }
  template<class A>Scanner&operator,(A&a) { a = Next<A>(); return *this; }
  template<class A>operator A() { return Next<A>(); }
}in;

/*---------------------------------------------------------------------*/
var(int, n, k);

struct Data {
  int a, b;
};
vector<Data> d(n);
ll solve1(int keta) {
  keta--;
  ll ans = 0;
  for (auto& i : d) {
    if ((i.a >> keta) == 0) {
      ans += i.b;
    }
  }
  return ans;
}

ll solve2(int k, int keta) {
  int mask = ~k & ((1 << keta) - 1);
  err(mask);
  ll ans = 0;
  for (auto& i : d) {
    if ((i.a & mask) == 0) {
      ans += i.b;
    }
  }
  return ans;
}

ll solve3(int keta) {
  vector<int> a(keta);
  int tmp = k;
  for (auto& i : a) {
    i = tmp & 1;
    tmp /= 2;
  }
  reverse(all(a));
  ll ans = 0;

  ll now = 0;
  err(a);
  rep(i, keta) {
    now = now << 1 | a[i];
    if (a[i] == 1 && i >= 1) {
      int am = keta - i;
      err(i, am, now, (now >> 2 << am << 1));
      amax(ans, solve2((now >> 2 << am << 1) | ((1 << am) - 1), keta));
    }
  }
  return ans;
}
void Calc() {


  for (auto& i : d) {
    in, i.a, i.b;
  }

  d.erase(std::remove_if(all(d), [](Data d) {
    return d.a > k;
  }), d.end());

  int tmp = k;
  int keta = 0;
  while (tmp) {
    tmp /= 2;
    keta++;
  }
  cout << (max({ solve1(keta), solve2(k, keta),solve3(keta) })) << endl;
}