-- 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 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] r = bar ks 0 0 0 b = r : a bar [] _ _ i = i bar (t:ts) v1 v2 i = a3 where j = i + 1 w1 = v1 + t w2 = v2 + t a1 = if w1 <= mn then bar ts w1 v2 j else 0 a2 = if w2 <= mx then bar ts v1 w2 j else 0 a3 = if w1 > mn && w2 > mx then i else maximum [a1, a2]