#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  map<long long int, long long int> tot, ct;
  long long int a[n], b[n];
  for (int i = 0 - (0 > n); i != n - (0 > n); i += 1 - 2 * (0 > n)) cin >> a[i];
  for (int i = 0 - (0 > n); i != n - (0 > n); i += 1 - 2 * (0 > n)) cin >> b[i];
  for (int i = 0 - (0 > n); i != n - (0 > n); i += 1 - 2 * (0 > n)) {
    tot[a[i]] += b[i];
    ct[a[i]]++;
  }
  long long int ans = 0;
  long long int ctt = 0;
  vector<long long int> aa, bb;
  for (auto e : tot) {
    if (ct[e.first] > 1) {
      ctt++;
      ans += e.second;
      aa.push_back(e.first);
    } else {
      bb.push_back(e.first);
    }
  }
  if (!ctt) {
    cout << 0 << "\n";
    return 0;
  }
  for (auto e : bb) {
    for (auto ee : aa) {
      if ((ee | e) == ee) {
        ans += tot[e];
        break;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
