#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<long long int, int>> peo(n);
  for (int i = 0; i < n; i++) cin >> peo[i].first;
  for (int i = 0; i < n; i++) cin >> peo[i].second;
  sort(peo.begin(), peo.end(), greater<pair<long long int, int>>());
  vector<bool> use(n, true);
  int i = 1;
  while (i < n && peo[i - 1].first != peo[i].first) use[i - 1] = false, i++;
  if (i == n) {
    cout << "0\n";
    return 0;
  }
  for (i++; i < n; i++) {
    bool res = false;
    for (int j = 0; j < n && peo[j].first >= peo[i].first; j++) {
      if (i != j && use[j])
        res |= ((peo[i].first & peo[j].first) == peo[i].first);
    }
    if (!res) use[i] = false;
  }
  long long int sum = 0;
  for (int i = 0; i < n; i++) sum += (use[i] ? peo[i].second : 0);
  cout << sum << '\n';
  return 0;
}
