#include <bits/stdc++.h>
using namespace std;
int n;
map<long long int, long long int> mp;
set<long long int> dup;
void init();
void process();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  init();
  process();
  cout.flush();
  return 0;
}
bool isSubset(long long int first, long long int second) {
  for (long long int i = 0; i < 60; i++) {
    if (((second >> i) & 1) == 1 && ((first >> i) & 1) == 0) return false;
  }
  return true;
}
void init() {
  cin >> n;
  vector<long long int> arr(n), brr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) cin >> brr[i];
  for (int i = 0; i < n; i++) {
    long long int first = arr[i], second = brr[i];
    if (mp.count(first) == 0)
      mp[first] = 0;
    else
      dup.insert(first);
    mp[first] += second;
  }
}
void process() {
  long long int ans = 0;
  for (auto u : dup) ans += mp[u];
  for (auto v : mp) {
    if (dup.count(v.first) != 0) continue;
    for (auto u : dup) {
      if (isSubset(u, v.first)) {
        ans += v.second;
        break;
      }
    }
  }
  cout << ans << '\n';
}
