#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 9;
const long long N = 500500;
map<long long, long long> f;
vector<long long> a, b;
long long n;
bool subset(long long a, long long b) {
  for (long long i = 0; i < 60; i++) {
    long long z1 = ((1LL << i) & a) > 0;
    long long z2 = ((1LL << i) & b) > 0;
    if (z1 == 1 && z2 == 0) return false;
  }
  return true;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    f[a[i]]++;
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
  }
  long long ans = 0;
  set<long long> s;
  for (long long i = 0; i < n; i++) {
    if (f[a[i]] >= 2) {
      ans += b[i];
      s.insert(a[i]);
    }
  }
  for (long long i = 0; i < n; i++) {
    if (f[a[i]] == 1) {
      for (auto z : s) {
        if (subset(a[i], z)) {
          ans += b[i];
          break;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
