import Data.Maybe

toBin :: Int -> [Bool]
toBin n
  | n == 0         = [False]
  | n `mod` 2 == 0 = False : toBin (n `div` 2)
  | n `mod` 2 == 1 = True  : toBin (n `div` 2)
  
included :: [Bool] -> [Bool] -> Bool
included []          bs2         = True
included (True :bs1) []          = False
included (False:bs1) []          = included bs1 []
included (True :bs1) (False:bs2) = False
included (_    :bs1) (_    :bs2) = included bs1 bs2

downGrade :: [Bool] -> Int -> Maybe [Bool]
downGrade bs i
  | i >= length bs   = Nothing
  | bs !! i == False = Nothing
  | bs !! i == True  =
      Just $ (take i (repeat True)) ++ (False : (drop (i + 1) bs))

allDownGrades :: [Bool] -> [[Bool]]
allDownGrades bs = bs : catMaybes (map (downGrade bs) [0..(length bs - 1)])

condSum :: [(Int, Int)] -> Int -> Int
condSum nvs cond =
  let dgs = allDownGrades (toBin cond)
  in maximum (map (f nvs) dgs)
  where f nvs dg = sum (map g nvs)
          where g (n, v) = if included (toBin n) dg then v else 0

getdata :: Int -> IO [(Int, Int)]
getdata n | n == 0    = return []
          | otherwise = do
              line <- getLine
              let (a : b : _) = words line
              abs <- getdata (n - 1)
              return ((read a, read b) : abs)

main :: IO ()
main = do
  line1 <- getLine
  let (n : k : _) = map read (words line1)
  nvs <- getdata n
  putStrLn $ show $ condSum nvs k
