{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString.Lazy.Char8 as LC solve :: LC.ByteString -> Bool solve = sol 0 0 0 . LC.reverse where sol :: Int -> Int -> Int -> LC.ByteString -> Bool sol w g r s | s == LC.empty = g == 0 && r == 0 | c == 'R' = sol w g (r + 1) cs | c == 'G' && r == 0 = False | c == 'G' = sol w (g + 1) (r - 1) cs | c == 'W' && g > 0 = sol (w + 1) (g - 1) r cs | c == 'W' && w == 0 = False | c == 'W' = sol (w + 1) g r cs | otherwise = sol w g r cs where c = LC.head s cs = LC.tail s main :: IO () main = LC.interact $ LC.unlines . fmap (f . solve) . tail . LC.lines where f True = "possible" f _ = "impossible"