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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := newScanner()
	N := sc.nextInt()
	K := sc.nextUint64()
	masks := make([]uint64, 10000)
	masks = append(masks, K)

	var i uint
	for i = 30; i >= 0; i-- {
		if (1<<i)&K != 0 {
			mask := K &^ (1 << i)
			mask = mask | (1<<i - 1)
			masks = append(masks, mask)
		}
		if i == 0 {
			break
		}
	}
	// for _, m := range masks {
	// 	fmt.Printf("%b \n", m)
	// }
	a := make([]uint64, N)
	b := make([]uint64, N)
	for i := 0; i < N; i++ {
		a[i] = sc.nextUint64()
		b[i] = sc.nextUint64()
	}
	anss := make([]uint64, len(masks))
	for i := 0; i < N; i++ {
		// fmt.Scan(&a, &b)
		// a := sc.nextUint64()
		// b := sc.nextUint64()
		for idx, m := range masks {
			if m|a[i] == m {
				anss[idx] += b[i]
			}
		}
	}
	var ans uint64
	for _, a := range anss {
		if a > ans {
			ans = a
		}
	}
	fmt.Println(ans)
}

type scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 10000)
	return &scanner{r: rdr}
}
func (s *scanner) next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *scanner) nextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *scanner) nextInt() int {
	v, _ := strconv.Atoi(s.next())
	return v
}
func (s *scanner) nextUint64() uint64 {
	v, _ := strconv.ParseUint(s.next(), 10, 64)
	return v
}
func (s *scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}