#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long PO(long long a, long long n) {
  long long val = 1;
  for (long long i = 0; i < n; i++) val *= a;
  return val;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return (a.second < b.second);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long bin[n][65];
  long long b[n];
  map<long long, long long> ma;
  map<long long, long long> tot_cst;
  map<long long, long long> marker;
  pair<long long, long long> ar[n];
  for (long long i = 0; i < n; i++) {
    cin >> ar[i].first;
  }
  for (long long i = 0; i < n; i++) {
    cin >> ar[i].second;
  }
  for (long long i = 0; i < n; i++) {
    ma[ar[i].first]++;
    tot_cst[ar[i].first] += ar[i].second;
  }
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    long long key = it->first;
    if ((it->second) >= 2) {
      marker[key] = 1;
      for (auto itr = ma.begin(); itr != ma.end(); itr++) {
        if (key == itr->first)
          continue;
        else {
          if (((key & itr->first) == itr->first) && key > itr->first) {
            marker[itr->first] = 1;
          }
        }
      }
    }
  }
  long long ans = 0;
  for (auto it = marker.begin(); it != marker.end(); ++it) {
    ans += tot_cst[it->first];
  }
  cout << ans << "\n";
}
