#include <bits/stdc++.h>
using namespace std;
template <typename a>
void SWAP(a &b, a &c) {
  a d = b;
  b = c;
  c = d;
}
long long power(long long a, long long b) {
  long long result = 1;
  while (b) {
    if (b & 1) result = a * result;
    a *= a;
    b >>= 1;
  }
  return result;
}
long long gcd(long long a, long long b) {
  while (b) {
    a = a % b;
    SWAP(a, b);
  }
  return a;
}
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
int main() {
  long long n;
  cin >> n;
  map<long long, long long> count;
  long long a[n], b[n], sum = 0;
  vector<long long> values;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (count.find(a[i]) == count.end()) count[a[i]] = 0;
    count[a[i]] = count[a[i]] + 1;
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
    if (count[a[i]] > 1) {
      sum += b[i];
    }
  }
  for (map<long long, long long>::iterator it = count.begin();
       it != count.end(); it++) {
    if ((long long)it->second > 1) {
      values.push_back((long long)it->first);
    }
  }
  for (long long i = 0; i < n; i++) {
    if (count[a[i]] == 1 && values.size() > 0) {
      bool result = false;
      for (long long j = 0; j < (long long)values.size(); j++) {
        result = true;
        long long val1, val2;
        val1 = a[i], val2 = values[j];
        while (val1 > 0 || val2 > 0) {
          long long c1, c2;
          c1 = val1 & 1;
          c2 = val2 & 1;
          if (c1 == 1 && c1 != c2) {
            result = false;
          }
          val1 = val1 >> 1;
          val2 = val2 >> 1;
        }
        if (result) break;
      }
      if (result) sum += b[i];
    }
  }
  cout << sum << endl;
  return 0;
}
