#include <bits/stdc++.h>
int main() {
  int n, t, pt;
  std::cin >> n;
  t = n * 10;
  pt = t + n;
  std::vector<int> f(t);
  std::vector<int> p(pt);
  for (int i = 0; i < t; ++i) std::cin >> f[i];
  for (int i = 0; i < pt; ++i) std::cin >> p[i];
  int ans[2];
  int c = 0;
  bool chk;
  for (int i = 0; i < 5; ++i) {
    ans[0] = ans[1] = 0;
    for (int j = 0; j < n; ++j) {
      chk = chk | f[i * j] | f[i * j + 1];
      ans[0] += p[i * j];
      ans[1] += p[i * j + 1];
    }
    if (chk) {
      c += ans[0] > ans[1] ? ans[0] : ans[1];
    }
  }
  std::cout << c;
  return 0;
}
