#include <bits/stdc++.h>
using namespace std;
template <typename... Args>
void print(Args&&... args) {
  (cout << ... << args) << '\n';
}
template <typename... Args>
void input(Args&&... args) {
  (cin >> ... >> args);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  constexpr uint64_t inf = numeric_limits<uint64_t>::max();
  int n;
  cin >> n;
  vector<uint64_t> a(n), b(n);
  for (auto& x : a) cin >> x;
  for (auto& x : b) cin >> x;
  map<uint64_t, uint64_t> s1;
  map<uint64_t, int> cnt1;
  for (int i = 0; i < n; ++i) {
    s1[a[i]] += b[i];
    cnt1[a[i]] += 1;
  }
  vector<int> cnt(n);
  for (int i = 0; i < n; ++i) cnt[i] = cnt1[a[i]];
  vector<bool> used(n);
  uint64_t ans = 0;
  while (true) {
    bool ok = false;
    uint64_t cur = inf;
    for (int i = 0; i < n; ++i)
      if (!used[i] && cnt[i] > 1 && (cur == inf || a[i] > cur)) {
        cur = a[i];
        ok = true;
      }
    if (!ok) break;
    for (int i = 0; i < n; ++i)
      if (!used[i] && ((a[i] | cur) == cur)) {
        used[i] = true;
        ans += b[i];
      }
  }
  cout << ans << '\n';
}
