let flood_bits bits =
  List.fold_left (fun bits shamt ->
    bits lor (bits lsr shamt)) bits
  [1; 2; 4; 8; 16; 32]
let highest_bit bits =
  bits land lnot (flood_bits bits lsr 1)

let rec generate_pattern k =
  if k = 0 then [0]
  else begin
    let highest = highest_bit k in
    highest - 1 :: List.map (( lor ) highest) (generate_pattern (k land lnot highest))
  end

let () =
  let n, k = Scanf.scanf "%d %d\n" (fun n k -> n, k) in
  let abs = Array.init n (fun _ -> Scanf.scanf "%d %d\n" (fun a b -> a, b)) in
  generate_pattern k
  |> List.map (fun pat ->
      Array.fold_left (fun b' (a, b) ->
        if a land lnot pat = 0 then b + b' else b') 0 abs)
  |> List.fold_left max 0
  |> Printf.printf "%d\n"