main :: IO ()
main = do
  nm <- getLine
  if nm == "0 0"
    then return ()
    else do
      ls <- getLine
      let ls' = map read . words $ ls
          [n,m] = map read .words $ nm
      print $ solve ls' m
      main

solve :: [Int] -> Int -> Int
solve ls m = package (sort ls) m
  where
    sort :: [Int] -> [Int]
    sort [] = []
    sort (x:xs) = (sort higher) ++ [x] ++ (sort lower)
      where
        higher = [p | p <- xs, p >= x]
        lower = [p | p <- xs, p < x]

    package :: [Int] -> Int -> Int
    package [] _ = 0
    package ls m = (takeSum ls m) + (package (rest m ls) m)
      where
        takeSum :: [Int] -> Int -> Int
        takeSum [] _ = 0
        takeSum _ 0 = 0
        takeSum _ 1 = 0
        takeSum (x:xs) n = x + (takeSum xs (n - 1))

        rest :: Int -> [Int] -> [Int]
        rest _ [] = []
        rest 0 xs = xs
        rest n (x:xs) = rest (n-1) xs