-- 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 n = n1 + n2 (u, v) = foldl (\(uu, vv) w -> if uu > n then (uu, vv) else (uu + w, vv + 1)) (0, 0) ss ks = take v ss [mn, mx] = sort [n1, n2] fff xx = foldl (\(rk, ki) vk -> let xk = rk + vk in if xk <= xx then (xk, ki + 1) else (rk ,ki)) (0, 0) ks (dk, di) = fff mn (h1, h2) = splitAt di ks ek = sum (0 : h2) r = case mn - dk of q | q == mn -> fi where (_, fi) = fff mx 0 -> if ek > mx then v - 1 else v _ -> if bar ss 0 then (if n < sum ks then v - 1 else v) else v - 1 where bar tts v1 = case compare v1 mn of EQ -> True GT -> False LT -> case tts of [] -> False t:ts -> (bar ts (v1 + t)) || (bar ts v1) b = r : a