import qualified Data.ByteString.Char8 as B import Data.Maybe (fromJust) import Data.List (sort) -- This recursive function naturally short-circuits (early exit). -- The moment `k >= x` is false, it stops and returns 0 without looking at the rest of `xs`. solve :: Int -> [Int] -> Int solve k (x:xs) | k >= x = 1 + solve (k - x) xs solve _ _ = 0 main :: IO () main = do -- 1. Read all input instantly as raw bytes (B.getContents) input <- B.getContents -- 2. B.words splits by any whitespace (newlines or spaces) -- 3. B.readInt parses integers directly from the byte array let (k:_:xs) = map (fst . fromJust . B.readInt) (B.words input) -- 4. Sort and solve print (solve k (sort xs))