#include <bits/stdc++.h>
using namespace std;
long long n, ansf;
long long a[7010], b[7010];
bool ex[7010];
vector<int> rq[7010];
bool rez(int nr) {
  for (auto i : rq[nr])
    if (ex[i])
      return 1;
    else {
      ex[i] = 1;
      bool ansa = rez(i);
      if (ansa) return 1;
      ex[i] = 0;
    }
  return 0;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j) {
        if ((a[i] & a[j]) == a[i]) rq[i].push_back(j);
      }
  for (int i = 1; i <= n; i++)
    if (!ex[i]) {
      ex[i] = 1;
      bool ansa = rez(i);
      if (!ansa) ex[i] = 0;
    }
  for (int i = 1; i <= n; i++) ansf += b[i] * ex[i];
  cout << ansf << '\n';
  return 0;
}
