<?php
fscanf(STDIN, "%d", $N);
$P = $F = [];
for ($n = 0; $n < $N; $n++) {
  $F[$n] = explode(' ', trim(fgets(STDIN)));
}
for ($n = 0; $n < $N; $n++) {
  $P[$n] = explode(' ', trim(fgets(STDIN)));
}

$total = 0;
$min = 0;
for ($n = 0; $n < $N; $n++) {
  $imax = 0;
  foreach ($F[$n] as $i) {
    $imax += $i == 1 ? 1 : 0;
  }

  $ep = $P[$n][0];
  for ($i = 1; $i <= $imax; $i++){
    $ep = $ep < $P[$n][$i] ? $P[$n][$i] : $ep;
    if ($min === 0) {
      $min = $P[$n][$i];
    } else {
      $min = $min < $P[$n][$i] ? $P[$n][$i] : $min;
    }
  }
  $total += $ep;
}

echo $total === 0 ? $min : $total . "\n";

?>
