import Data.List (sort) main :: IO () main = do input <- getContents let (k:_:xs) = map read (words input) -- 1. sort xs -> Sort items lowest to highest -- 2. scanl (+) 0 -> Create running totals: [0, a1, a1+a2, ...] -- 3. takeWhile (<= k) -> Keep only the totals within budget -- 4. length (...) - 1 -> Count them (minus 1 for the initial 0) print $ length (takeWhile (<= k) (scanl (+) 0 (sort xs))) - 1