-- Try yukicoder -- author: Leonardone @ NEETSDKASU import Data.List (any, 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 = case compare v1 mn of EQ -> (True, v1, oo ++ tts) GT -> (False, v1 - t1, t1 : oo ++ tts) LT -> case tts of [] -> (False, v1, oo) t:ts -> rv where (wb, wv, ws) = bar ts (v1 + t) oo t (qb, qv, qs) = bar ts v1 (t : oo) t1 rv = if wb then (wb, wv, ws) else if qb then (qb, qv, qs) else if wv > qv then (wb, wv, ws) else (qb, qv, qs) (rb, rv, rs) = bar ms 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