#include <bits/stdc++.h>
using namespace std;
bool superior(long long a, long long b) {
  if (a > b) return 1;
  while (a) {
    if ((a & 1) > (b & 1)) return 1;
    a >>= 1;
    b >>= 1;
  }
  return 0;
}
pair<long long, long long> a[7777];
queue<long long> q;
vector<pair<long long, long long> > b;
vector<long long> v;
long long curr, ans;
int sel[7777];
int i, j, n;
bool OK(long long X) {
  for (i = 0; i < v.size(); ++i)
    if (superior(X, v[i])) return 0;
  return 1;
}
int main() {
  cin >> n;
  for (i = 0; i < n; ++i) cin >> a[i].first;
  for (i = 0; i < n; ++i) cin >> a[i].second;
  sort(a, a + n);
  for (i = 1; i < n; ++i)
    if (a[i].first == a[i - 1].first) {
      v.push_back(a[i].first);
      ans += a[i].second + a[i - 1].second;
      sel[i] = sel[i - 1] = 1;
      q.push(a[i].first);
      j = i + 1;
      while (j < n && a[j].first == a[j - 1].first) {
        ans += a[j].second;
        sel[j] = 1;
        ++j;
      }
      i = j - 1;
    } else
      b.push_back(a[i]);
  while (!q.empty()) {
    curr = q.front();
    q.pop();
    for (i = 0; i < n; ++i)
      if (!sel[i] && !superior(a[i].first, curr)) {
        ans += a[i].second;
        q.push(a[i].first);
        sel[i] = 1;
      }
  }
  cout << ans;
  return 0;
}
