#include <bits/stdc++.h>
using namespace std;
bool is_ok(vector<int64_t> taken, int64_t val) {
  for (auto j : taken)
    if ((j & val) == val) return true;
  return false;
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int64_t n, i, ans;
  cin >> n;
  pair<int64_t, int64_t> a[n];
  map<int64_t, int64_t> cnt;
  for (int64_t i = 0; i < n; i++) {
    cin >> a[i].first;
    cnt[a[i].first]++;
  }
  for (int64_t i = 0; i < n; i++) cin >> a[i].second;
  ans = 0;
  vector<int64_t> taken;
  vector<pair<int64_t, int64_t>> rem;
  map<int64_t, bool> vis;
  for (int64_t i = 0; i < n; i++) {
    if (cnt[a[i].first] > 1) {
      ans += a[i].second;
      if (!vis[a[i].first]) taken.push_back(a[i].first);
      vis[a[i].first] = true;
    } else
      rem.push_back(a[i]);
  }
  for (auto j : rem) {
    if (is_ok(taken, j.first)) {
      taken.push_back(j.first);
      ans += j.second;
    }
  }
  cout << ans << '\n';
  return 0;
}
