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

    let vP = VU.snoc (mySort aP aX) 0
    let vQ = VU.snoc (mySort aQ aY) 0
    let vR = VU.snoc (mySort aR (aX + aY)) 0
    putStrLn $ show
        (sumApple (trace (show vP) vP)
                  (trace (show vQ) vQ)
                  (trace (show vR) vR)
                  aX
                  aY
                  0
        )

mySort :: VU.Vector Int -> Int -> VU.Vector Int
mySort xs c = VU.foldl (\acc x -> scan acc x c) (VU.take 1 xs) (VU.tail xs)

scan :: VU.Vector Int -> Int -> Int -> VU.Vector Int
scan xs target c =
    let
        lxs         = VU.length xs
        numlist     = [0 .. lxs - 1]
        insPosition = foldl
            (\acc x -> if acc == c
                then if ((xs VU.! x) < target) then x else c
                else acc
            )
            c
            numlist
        insVec =
            (VU.snoc (VU.take (insPosition) xs) target)
                VU.++ (VU.drop (insPosition) xs)
    in
        if (VU.length xs) > c then VU.init insVec else insVec

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




sumApple
    :: VU.Vector Int
    -> VU.Vector Int
    -> VU.Vector Int
    -> Int
    -> Int
    -> Int
    -> Int
sumApple reds greens nils tX tY tZ
    | (tX + tY) == tZ
    = 0
    | otherwise
    = if (maxNum == red) && (tX > 0)
              then
                  trace "red"
                  $ red
                  + (sumApple (VU.tail reds) greens nils (tX - 1) tY tZ)
              else if ((max green nil) == green) && (tY > 0)
                  then
                      trace "green"
                      $ green
                      + (sumApple reds (VU.tail greens) nils tX (tY - 1) tZ)
                  else
                      trace "nul"
                      $ nil
                      + (sumApple reds greens (VU.tail nils) tX tY (tZ + 1))

  where
    red    = if tX > 0 then VU.head reds else 0
    green  = if tY > 0 then VU.head greens else 0
    nil    = if tZ >0 then VU.head nils else 0
    maxNum = max red (max green nil)
