{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad
import Control.Monad.Fix
import Data.Int
import Data.Foldable
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.ByteString.Char8 as B

ifoldl' :: (b -> Int -> a -> b) -> b -> [a] -> b
ifoldl' !f !a !xs = foldl' (\(!acc) (!i, !x) -> f acc i x) a $ zip [0 ..] xs

solve :: Int -> Int -> Int -> Int -> VU.Vector Int -> Int
solve !n !a !b !c !vn = minimum
  $ map (\(!ga, !gb, !gc) -> cost a ga + cost b gb + cost c gc) groups
 where
  indices = replicateM n "abcd"
  groups =
    filter (\(!a, !b, !c) -> not (null a) && not (null b) && not (null c)) $ map
      (ifoldl'
        (\(!ga, !gb, !gc) (!i) (!ch) -> case ch of
          'a' -> (vn VU.! i : ga, gb, gc)
          'b' -> (ga, vn VU.! i : gb, gc)
          'c' -> (ga, gb, vn VU.! i : gc)
          'd' -> (ga, gb, gc)
        )
        ([], [], [])
      )
      indices

  cost k xs = countComposition (length xs) * 10 + abs (sum xs - k)

  countComposition n | n <= 1    = 0
                     | otherwise = n `div` 2 + countComposition (n - n `div` 2)

main = do
  let readInt :: Integral a => B.ByteString -> Maybe (a, B.ByteString)
      readInt = fmap (first fromIntegral . second B.tail) . B.readInt

  (n, a, b, c) <-
    (\vec -> (vec VU.! 0, vec VU.! 1, vec VU.! 2, vec VU.! 3))
    .   VU.unfoldrN 4 readInt
    <$> B.getLine
  vn <-
    VU.replicateM n $ (\vec -> vec VU.! 0) . VU.unfoldrN 1 readInt <$> B.getLine

  print $ solve n a b c vn
