import Control.Applicative ((<$>)) import qualified Data.ByteString.Char8 as C import Data.Hashable (Hashable) import qualified Data.HashMap.Strict as HS import Data.Maybe (fromJust) type Count = Int type Counter a = HS.HashMap a Count addElement :: (Hashable a, Eq a) => a -> Counter a -> Counter a addElement x = HS.insertWith (+) x 1 buildCharCounter :: C.ByteString -> Counter Char buildCharCounter = C.foldr addElement HS.empty countElement :: (Hashable a, Eq a) => Counter a -> a -> Count countElement = flip $ HS.lookupDefault 0 unsafeReadInt :: C.ByteString -> Int unsafeReadInt = fst . fromJust . C.readInt calcScore :: Int -> Int -> Int -> Counter Char -> Int calcScore g0 c0 p0 ctr = 3 * (gWin + cWin + pWin) + gDraw + cDraw + pDraw where gZ0 = countElement ctr 'G' cZ0 = countElement ctr 'C' pZ0 = countElement ctr 'P' gWin = min g0 cZ0 cWin = min c0 pZ0 pWin = min p0 gZ0 gZ = gZ0 - pWin cZ = cZ0 - gWin pZ = pZ0 - cWin g = g0 - gWin c = c0 - cWin p = p0 - pWin gDraw = min g gZ cDraw = min c cZ pDraw = min p pZ main :: IO () main = do [g, c, p] <- fmap unsafeReadInt . C.words <$> C.getLine ctr <- buildCharCounter <$> C.getLine print $ calcScore g c p ctr