-- 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 (wb, wv, ws, wi) = bar ts (v1 + t) oo t (i + 1) (qb, qv, qs, qi) = bar ts v1 (t : oo) t1 i rv = case True of _ | wb -> (wb, wv, ws, wi) _ | qb -> (qb, qv, qs, qi) _ | wi > qi -> (wb, wv, ws, wi) _ | wi < qi -> (qb, qv, qs, qi) _ | wv > qv -> (wb, wv, ws, wi) _ -> (qb, qv, qs, qi) (_, _, 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