fun readInt () = valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn) fun quicksort [] = [] | quicksort (h::tl) = let val (s, b) = List.foldl (fn (x, (small, big)) => if x <= h then (x::small, big) else (small, x::big)) ([], []) tl in (quicksort s) @ [h] @ (quicksort b) end fun takeOutCandies _ [] = 0 | takeOutCandies numToTake (h::tl) = if h = numToTake then List.length tl else if h < numToTake then takeOutCandies (numToTake - h) tl else List.length (h::tl) val () = let val n = readInt () val m = readInt () val c_s = List.tabulate (n, fn _ => readInt ()) val ans = n - takeOutCandies m (quicksort c_s) in print (Int.toString ans ^ "\n") end