import Data.List

solve (n:m:rest) =
    sum $ map toInteger $ merge
    where
        split arr = case arr of 
            [] -> []
            (b:c:rest) -> (c,b) : split rest
        (f,r) = splitAt n rest
        cs = (reverse.sort $ split r) >>= (uncurry $ flip replicate)
        merge = zipWith max (sort f) (cs ++ repeat 0)

main = interact $ show.solve.map read.words