#include <bits/stdc++.h>
using namespace std;
bool is_better(unsigned long long a, unsigned long long b) {
  return (a & b) < a;
}
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<unsigned long long> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  vector<vector<int>> g(n);
  vector<int> deg(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (!is_better(a[i], a[j])) {
        g[j].push_back(i);
        ++deg[i];
      }
      if (!is_better(a[j], a[i])) {
        g[i].push_back(j);
        ++deg[j];
      }
    }
  }
  queue<int> q;
  for (int i = 0; i < n; ++i) {
    if (deg[i] == 0) {
      q.push(i);
    }
  }
  vector<int> out(n);
  while (!q.empty()) {
    int x = q.front();
    out[x] = 1;
    q.pop();
    for (int i : g[x]) {
      --deg[i];
      if (deg[i] == 0) {
        q.push(i);
      }
    }
  }
  unsigned long long res = 0;
  for (int i = 0; i < n; ++i) {
    if (!out[i]) {
      res += b[i];
    }
  }
  cout << res << '\n';
}
