#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
mt19937_64 rng((unsigned long long)new char);
const long long INF = 1ll << 58;
const int maxn = 7e3 + 10;
int mod = 1e9 + 7;
int n;
long long a[maxn], b[maxn];
inline bool in(long long& a, long long& b) { return (a & b) == a; }
struct no {
  long long a, b;
  bool mul, er;
};
map<long long, pair<long long, bool>> w;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int t = (0); t < (n); ++t) cin >> a[t];
  for (int t = (0); t < (n); ++t) cin >> b[t];
  for (int i = (0); i < (n); ++i) {
    if (w.count(a[i])) {
      w[a[i]].first += b[i], w[a[i]].second = 1;
    } else {
      w[a[i]] = {b[i], 0};
    }
  }
  vector<no> v;
  for (auto& e : w) v.push_back({e.first, e.second.first, e.second.second, 0});
  reverse(v.begin(), v.end());
  long long ans = 0;
  for (int t = (0); t < (v.size()); ++t) {
    auto& c = v[t];
    if (!c.er && c.mul) {
      ans += c.b;
      for (int i = (t + 1); i < (v.size()); ++i) {
        auto& d = v[i];
        if (!d.er && in(d.a, c.a)) {
          d.er = 1;
          ans += d.b;
        }
      }
    }
  }
  cout << ans;
}
