#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, pos[7007];
ll a[7007], b[7007], res;
vector<pair<ll, ll>> group, single;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  if (n == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; ++i) cin >> a[i], pos[i] = i;
  for (int i = 1; i <= n; ++i) cin >> b[i];
  sort(pos + 1, pos + 1 + n,
       [](const int &x, const int &y) { return a[x] < a[y]; });
  ll curb = b[pos[1]];
  bool flag = false;
  for (int i = 2; i <= n; ++i) {
    if (a[pos[i]] != a[pos[i - 1]]) {
      if (flag)
        group.emplace_back(a[pos[i - 1]], curb);
      else
        single.emplace_back(a[pos[i - 1]], curb);
      curb = 0;
      flag = false;
    } else {
      flag = true;
    }
    curb += b[pos[i]];
  }
  if (flag)
    group.emplace_back(a[pos[n]], curb);
  else
    single.emplace_back(a[pos[n]], curb);
  if (group.empty()) {
    cout << 0;
    return 0;
  }
  vector<ll> s;
  for (auto x : group) res += x.second, s.push_back(x.first);
  for (auto x : single) {
    ll &xa = x.first;
    bool flag = false;
    for (ll aa : s) {
      bool flag2 = true;
      for (int k = 0; k < 60; ++k) {
        if ((xa & (1LL << k)) && !(aa & (1LL << k))) {
          flag2 = false;
          break;
        }
      }
      if (flag2) {
        flag = true;
        break;
      }
    }
    if (flag) res += x.second;
  }
  cout << res;
  return 0;
}
