import Control.Monad
main=do
 n<-readLn
 f<-replicateM n (map read.words<$>getLine)
 p<-replicateM n (map read.words<$>getLine)
 print(g (n-1) f p (-100000000) 1023)
  where g _ _ _ m 0 = m
        g n f p m i = g n f p (max m (h n p (j f (k i)))) (i-1)
        k i = [quot(mod i (2^x))(2^(x-1))|x<-[1..10]]
        j f i =[sum((map (\l->(fst l)*(snd l)))(zip x i))|x<-f]
        h n p f = sum[(p!!x)!!(f!!x)|x<-[0..n]]