ans 0 0 acc = Just acc ans l _ acc | l <= 0 = Nothing ans l s acc | 0 < l && s == 0 = ans (l - 1) s ('C' : acc) ans l s acc = let v = ans (l - 1) (s - 1) ('A' : acc) in case v of Just val -> Just val Nothing -> ans (l - 1) (s - 2) ('B' : acc) main = interact $ answer . map (\x -> read x :: Int) . words . head . lines where answer [n, m] = case ans n m [] of Just val -> (show . reverse) val ++ "\n" Nothing -> "\n"