import Control.Arrow
import Data.Char
import Data.List
import qualified Data.ByteString.Char8 as B
main=print.solve.unfoldr(B.readInt.B.dropWhile isSpace)=<<B.getContents
solve(n:k:as)=foldr(+)0.uncurry take.((subtract k).length&&&sort).map length.group.sort$as