module A = Array
module C = Char
module I = Int64
module L = List
module Q = Queue

let pf = Printf.printf
let sf = Scanf.scanf
let ssf = Scanf.sscanf

let read_int () = sf "%d " (fun x -> x)
let read_float () = sf "%f " (fun x -> x)
let read_str () = sf "%s " (fun x -> x)
let read_array n read () = A.init n (fun _ -> read ())
let err s = raise (Failure s)

let range s t = A.init (t - s) @@ (+) s
let foreach fold_f init s t map_f =
  range s t |> A.map map_f |> A.fold_left fold_f init

let inf = int_of_float 1e18
let eps = 1e-11

module S = struct
  include String
  let of_array a = String.init (A.length a) (A.get a)
  let to_array s = A.init (String.length s) (String.get s)
end;;

let rec sum_of_digit n =
  if n = 0
  then 0
  else sum_of_digit (n / 10) + n mod 10

let () =
  let n = read_int () in
  let f = read_array n (read_array 10 read_int) () in
  let p = read_array n (read_array 11 read_int) () in
  let res =
    let loop1 bit =
      let loop2 f p =
        let loop3 t = (bit lsr t) land f.(t) in
        let cnt = foreach (+) 0 0 10 loop3 in
        p.(cnt) in
      A.map2 loop2 f p |> A.fold_left (+) 0 in
    foreach max (-inf) 1 1024 loop1 in
  pf "%d\n" res
