module Main where

import           Debug.Trace
import qualified Data.Vector.Unboxed           as VU
import           Data.List
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char


getVUIntFromInputLineAtBSC8 :: IO (VU.Vector Int)
getVUIntFromInputLineAtBSC8 =
    VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
getListIntFromInputLineAtBSC8 =
    unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
main :: IO ()
main = do
    [aX, aY, aA, aB, aC] <- getListIntFromInputLineAtBSC8
    aP                   <- getVUIntFromInputLineAtBSC8
    aQ                   <- getVUIntFromInputLineAtBSC8
    aR                   <- getVUIntFromInputLineAtBSC8

    putStrLn $ show
        (sumApple
            (VU.fromList
                (reverse
                    (sortMerged
                        (  (sortConved
                               (VU.toList
                                   (VU.zip aP $ VU.fromList $ replicate
                                       (VU.length aP)
                                       1
                                   )
                               )
                               aX
                           )
                        ++ (sortConved
                               (VU.toList
                                   (VU.zip aQ $ VU.fromList $ replicate
                                       (VU.length aQ)
                                       2
                                   )
                               )
                               aY
                           )
                        ++ (sortConved
                               (VU.toList
                                   (VU.zip aR $ VU.fromList $ replicate
                                       (VU.length aR)
                                       3
                                   )
                               )
                               (aX + aY)
                           )
                        )
                    )
                )
            )
            aX
            aY
            0
        )

sortConved :: [(Int, Int)] -> Int -> [(Int, Int)]
sortConved []       _ = []
sortConved (x:[])        1 = [x]
sortConved (x : xs) c = if lgteq == c
    then (sortConved gteq (c))
    else if lgteq > c
        then (sortConved gteq c)
        else (sortConved lt (c - lgteq)) ++ [x] ++ (sortConved gteq lgteq)
  where
    lt    = filter (\y -> (fst y) <= (fst x)) xs
    gteq  = filter (\y -> (fst y) > (fst x)) xs
    lgteq = length gteq

sortMerged :: [(Int, Int)] -> [(Int, Int)]
sortMerged []       = []
sortMerged (x : xs) = (sortMerged lt) ++ [x] ++ (sortMerged gteq)
  where
    lt   = filter (\y -> (fst y) <= (fst x)) xs
    gteq = filter (\y -> (fst y) > (fst x)) xs



sumApple :: VU.Vector (Int, Int) -> Int -> Int -> Int -> Int
sumApple m tX tY tZ = trace (show m) ret
  where
    (_, _, _, ret) = VU.foldl
        (\(fX, fY, fZ, s) x -> if (fX + fY) == fZ
            then (fX, fY, fZ, s)
            else case (snd x) of
                1 -> trace "1" $ if fX > 0
                    then (fX - 1, fY, fZ, s + (fst x))
                    else (fX, fY, fZ, s)
                2 -> trace "2" $ if fY > 0
                    then (fX, fY - 1, fZ, s + (fst x))
                    else (fX, fY, fZ, s)
                3         -> trace "3" $ (fX, fY, fZ + 1, s + (fst x))
                otherwise -> (fX, fY, fZ, s)
        )
        (tX, tY, tZ, 0)
        m
