#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct info {
  ll a;
  int b;
  bool operator<(const info &src) const { return a > src.a; }
};
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<info> peo(n);
  for (int i = 0; i < n; ++i) cin >> peo[i].a;
  for (int i = 0; i < n; ++i) cin >> peo[i].b;
  sort(peo.begin(), peo.end());
  vector<info> ansvec;
  for (int i = 0; i < n; ++i) {
    if (i != n - 1 && peo[i].a == peo[i + 1].a) {
      ansvec.push_back(peo[i]);
      continue;
    }
    for (const auto &sonele : ansvec) {
      if ((sonele.a | peo[i].a) <= sonele.a) {
        ansvec.push_back(peo[i]);
        break;
      }
    }
  }
  ll ans = 0;
  for (const auto &ele : ansvec) ans += ele.b;
  cout << ans << endl;
  return 0;
}
