Scanf.scanf "%d %d" (fun n k ->
    let ab = Array.init n (fun _ -> Scanf.scanf " %d %d" (fun a b -> a, b)) in

    let check k2 =
        Array.fold_left (fun acc (a, b) ->
            if k2 land a = a then acc + b else acc) 0 ab
    in

    let rec loop i acc =
        let l = 1 lsl i in
        if l > k then acc else
            if k land l = 0 then loop (i + 1) acc else
                let k2 = (k lxor l) lor (l - 1) in
                loop (i + 1) (max acc (check k2))
    in
    loop 0 (check k) |> Printf.printf "%d\n"
)