#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll M = 1000000007;
int n;
int main() {
  cin >> n;
  vector<ll> a(n);
  vector<int> b(n);
  for (int i = 0; i < (int)(n); i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < (int)(n); i++) scanf("%d", &b[i]);
  vector<bool> use(n, false);
  deque<ll> stu;
  map<ll, int> appeared;
  bool ok = false;
  vector<bool> done(60, false);
  for (int i = 0; i < (int)(n); i++) {
    for (int j = (i + 1); j < (n); ++j) {
      if (i != j) {
        if (a[i] == a[j]) {
          ok = true;
          use[i] = use[j] = true;
          if (appeared[a[i]] == 0) {
            stu.push_back(a[i]);
            appeared[a[i]]++;
          }
        }
      }
    }
  }
  if (ok) {
    ll sum = 0;
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(stu.size()); j++) {
        if ((a[i] | stu[j]) == stu[j]) {
          sum += b[i];
          j = stu.size();
        }
      }
    }
    cout << sum << endl;
  } else {
    cout << 0 << endl;
  }
}
