import Control.Monad
import Control.Applicative
import Data.List
import qualified Data.Map as Map
import Data.Array
import qualified Data.ByteString.Char8 as BC
import Data.Maybe
readInts = map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine
readInt :: BC.ByteString -> Int
readInt = fst . fromJust . BC.readInt
strToInt s = (read :: String -> Int) s

main = do
    n <- readLn :: IO Int
    fsn <- map ((map readInt . BC.words)) <$> replicateM n BC.getLine
    psn <- map ((map readInt . BC.words)) <$> replicateM n BC.getLine
    let fs = listArray (0,(n-1)) fsn
    let ps = listArray (0,(n-1)) psn

    let cd = [0,1]
    let css = listArray (0,4091) [[c0,c1,c2,c3,c4,c5,c6,c7,c8,c9]|c0 <- cd , c1 <- cd , c2 <- cd , c1 <- cd , c3 <- cd , c1 <- cd , c4 <- cd , c5 <- cd , c6 <- cd , c7 <- cd , c8 <- cd , c9 <- cd ,[c0,c1,c2,c3,c4,c5,c6,c7,c8,c9] /= [0,0,0,0,0,0,0,0,0,0] ]
    
    let ans = [profit fs ps (css!i) n | i<-[0..4091]]
    print (maximum ans)
    
profit fs ps cs n  = sum [profita fs ps cs i | i <- [1..n]]
profita fs ps cs i = (ps ! (i-1)) !! n
                     where n = count (fs ! (i-1)) cs 0


count :: [Int] -> [Int] -> Int -> Int
count [] bn n = n
count (a:an) (b:bn) n = if (a==1) && (b==1)
                        then 1 + (count an bn n)
                        else (count an bn n)
