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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


public class Main {

  static class Node {
    Node node0;
    Node node1;
    long sum = -1;
    
    public void add(long x, long value, int depth) {
      if (depth < 0) {
        this.sum = value;
        return;
      }

      if ((x & (1L << depth)) != 0) {
        if (node1 == null) {
          node1 = new Node();
        }
        node1.add(x, value, depth - 1);
      } else {
        if (node0 == null) {
          node0 = new Node();
        }
        node0.add(x, value, depth - 1);
      }
    }
    
    public long sum() {
      if (sum < 0) {
        sum = 0;
        if (node1 != null) {
          sum +=  node1.sum();
        }
        if (node0 != null) {
          sum += node0.sum();
        }
      }
      return sum;
    }
    
    public long max(long k, int depth) {
      if (depth < 0) {
        return this.sum;
      }
      if ((k & (1L << depth)) != 0) {
        long a = node0 == null ? 0 : node0.sum;
        long b = node0 == null ? 0 : node0.max(k, depth - 1);
        long c = node1 == null ? 0 : node1.max(k, depth - 1);
        return Math.max(a, b + c);
      } else {
        long b = node0 == null ? 0 : node0.max(k, depth - 1);
        return b;
      }
    }
  }
  
  private static void solve() {
    int n = ni();
    int k = ni();
    int[][] a = ntable(n, 2);
    
    Node root = new Node();
    for (int[] v : a) {
      root.add(v[0], v[1], 32);
    }
    root.sum();
    

    System.out.println(root.max(k, 32));
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = args.length > 0 ? args[0] : null;
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}