#include <bits/stdc++.h>
using namespace std;
using pii = pair<long long int, long long int>;
using vi = vector<long long int>;
using vpii = vector<pii>;
using G = vector<vi>;
using WG = vector<vpii>;
const long long int INF = 0x3f3f3f3f;
const long long int mod = 1e9 + 7;
const long long int mod2 = 1e9 + 9;
int32_t main() {
  long long int n;
  cin >> n;
  vi a(n);
  for (long long int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vi b(n);
  for (long long int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  auto check = [](long long int x, long long int y) {
    while (x) {
      if ((x & 1) && (~y & 1)) {
        return true;
      }
      x >>= 1LL;
      y >>= 1LL;
    }
    return false;
  };
  vi dgo(n);
  vector<vector<int32_t>> g(n);
  for (long long int i = 0; i < n; ++i) {
    for (long long int j = 0; j < n; ++j)
      if (i != j) {
        if (check(a[i], a[j])) {
          ++dgo[i];
          g[j].push_back(i);
        }
      }
  }
  long long int t = n - 1;
  vi can(n, 1);
  while (true) {
    long long int q = 0;
    vi buf;
    for (long long int i = 0; i < n; ++i) {
      if (dgo[i] == t) {
        can[i] = 0;
        buf.push_back(i);
        ++q;
      }
    }
    for (long long int i : buf) {
      for (long long int x : g[i]) {
        --dgo[x];
      }
    }
    if (q == 0) {
      break;
    }
    t -= q;
  }
  long long int ans = 0, qtd = 0;
  for (long long int i = 0; i < n; ++i) {
    if (can[i] == 1) {
      ans += b[i];
      ++qtd;
    }
  }
  if (qtd == 1) {
    ans = 0;
  }
  cout << ans << '\n';
}
