import Control.Monad

main = do
  n <- readLn :: IO Int
  f <- (replicateM n getLine) >>= return . map ((map read) . words) :: IO [[Int]]
  p <- (replicateM n getLine) >>= return . map ((map read) . words) :: IO [[Int]]

  let
--    func :: [Int] -> Int
    func xs = sum [p!!(i-1)!!(c!!(i-1)) | i<-[1..n]]
      where
        c = [sum [(xs!!t) * (f!!(i-1)!!t) | j<-[1..5], k<-[1,2], let t = 2*j-3+k] | i<-[1..n]]

  print $ maximum $ map func (tail (patt 10))

  where
    patt :: Int -> [[Int]]
    patt 0 = [[]]
    patt n = concat [map (i:) (patt (n-1)) | i<-[0,1]]
