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
import           Data.Maybe


getVUIntFromInputLineAtBSC8 :: IO (VU.Vector Int)
getVUIntFromInputLineAtBSC8 =
    VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
getListIntFromInputLineAtBSC8 =
    unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
readInt = fst . fromJust . BSC8.readInt
readIntList = map readInt . BSC8.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main :: IO ()
main = do
    [aX, aY, aA, aB, aC] <- getListIntFromInputLineAtBSC8
    --aP                   <- getVUIntFromInputLineAtBSC8
    --aQ                   <- getVUIntFromInputLineAtBSC8
    --aR                   <- getVUIntFromInputLineAtBSC8

    vP <- VU.fromList . take aX . reverse . sort <$> getIntList
    vQ <- VU.fromList . take aY . reverse . sort <$> getIntList
    vR <- VU.fromList . take (aX + aY) . reverse . sort <$> getIntList

    putStrLn $ show
        (sumApple (VU.snoc vP 0) (VU.snoc vQ 0) (VU.snoc vR 0) aX aY 0)

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

scan :: VU.Vector Int -> Int -> Int -> VU.Vector Int
scan xs target c = if insPosition < c
    then if VU.length insVec > c then VU.unsafeInit insVec else insVec
    else xs
  where
    lastPosition = min (VU.length xs - 1) (c - 1)
    insPosition  = (search xs target c lastPosition) + 1
    lxs          = VU.length xs
    numlist      = [0 .. lxs - 1]
    insVec =
        (VU.snoc (VU.unsafeTake (insPosition) xs) target)
            VU.++ (VU.unsafeDrop (insPosition) xs)

search :: VU.Vector Int -> Int -> Int -> Int -> Int
search xs target c p
    | p == -1   = p
    | otherwise = if (xs VU.! p) < target then search xs target c (p - 1) else p

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 red + (sumApple (VU.unsafeTail reds) greens nils (tX - 1) tY tZ)
        else if ((max green nil) == green) && (tY > 0)
            then
                green
                    + (sumApple reds (VU.unsafeTail greens) nils tX (tY - 1) tZ)
            else
                nil + (sumApple reds greens (VU.unsafeTail 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    = VU.head nils
    maxNum = max red (max green nil)


Measured memory_bytes: 27004