#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug &operator<<(const c &) {
    return *this;
  }
};
const long long N = 7e3 + 5;
long long n, a[N], b[N];
bool used[N];
map<long long, long long> mp;
bool smaller(long long x, long long y) {
  long long tmp = max(x, y);
  long long cur = 1;
  while (cur <= tmp) {
    if ((y & cur) && !(x & cur)) return false;
    cur = cur << 1;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i]]++;
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
  }
  long long sum = 0;
  for (auto j : mp) {
    if (j.second >= 2) {
      debug() << " ["
              << "j"
                 ": "
              << (j) << "] ";
      long long tmp = j.first;
      for (long long i = 0; i < n; i++) {
        if ((tmp == a[i] || smaller(tmp, a[i])) && !used[i]) {
          sum += b[i];
          used[i] = true;
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}
