#include <bits/stdc++.h>
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
using namespace std;
long long n, a[7005], b[7005];
int out[7005];
vector<int> radj[7005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long i, j, m, k, cnt = 0, ans = 0, t = 1;
  cin >> n;
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n; i++) cin >> b[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (i == j) continue;
      if ((a[i] & a[j]) == a[i]) {
        radj[j].push_back(i);
        out[i]++;
      }
    }
  }
  queue<int> q;
  for (i = 0; i < n; i++) {
    if (out[i] == 0) q.push(i);
  }
  set<int> s;
  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    s.insert(u);
    for (auto it : radj[u]) {
      out[it]--;
      if (out[it] == 0) q.push(it);
    }
  }
  for (i = 0; i < n; i++) {
    if (!s.count(i)) ans += b[i];
  }
  return cout << ans, 0;
  return 0;
}
