-- Try yukicoder -- author: Leonardone @ NEETSDKASU import Data.List (sort) main = putStrLn . unlines . solve . map read . tail . words =<< getContents solve x = map show . reverse $ foo [] x where foo a [] = a foo a (n1:n2:m:xs) = foo b zs where (ys, zs) = splitAt m xs ss = sort ys [mn, mx] = sort [n1, n2] (ms, fs) = span (<= mn) ss bar tts v1 oo t1 i = case compare v1 mn of EQ -> (True, v1, oo ++ tts, i) GT -> (False, v1 - t1, t1 : oo ++ tts, i - 1) LT -> case tts of [] -> (False, v1, oo, i) t:ts -> rv where ww = bar ts (v1 + t) oo t (i + 1) (wb, wv, ws, wi) = ww qq = bar ts v1 (t : oo) t1 i (qb, qv, qs, qi) = qq rv = case True of _ | wb -> ww _ | wi > qi -> ww _ | wi < qi -> qq _ | qb -> qq _ | wv > qv -> ww _ -> qq (_, _, rs, _) = bar ms 0 [] 0 0 (_ , ei) = foldl (\(dv, di) v -> let u = dv + v in if u > mx then (dv, di + 1) else (u, di)) (0, 0) . sort $ rs ++ fs b = m - ei : a