#include <bits/stdc++.h>
using namespace std;
const long long MX = 7005;
struct Data {
  long long skill;
  long long x;
  long long degree;
  long long ind;
};
Data A[MX];
bool cmp(const Data &D1, const Data &D2) { return D1.degree > D2.degree; }
vector<int> adj[MX];
long long pos[MX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  long long ans = 0;
  for (long long i = 0; i < (long long)n; i++) {
    cin >> A[i].x;
  }
  for (long long i = 0; i < (long long)n; i++) {
    cin >> A[i].skill;
    ans += A[i].skill;
    A[i].ind = i;
  }
  for (long long i = 0; i < (long long)n; i++) {
    for (long long j = 0; j < (long long)n; j++) {
      if (((A[i].x ^ A[j].x) & (A[i].x)) > 0) {
        A[i].degree++;
        adj[j].push_back(i);
      }
    }
  }
  sort(A, A + n, cmp);
  for (long long i = 0; i < (long long)n; i++) {
    pos[A[i].ind] = i;
  }
  long long total = n;
  for (long long i = 0; i < (long long)n; i++) {
    if (A[i].degree >= total - 1) {
      ans -= A[i].skill;
      for (long long v : adj[A[i].ind]) {
        A[pos[v]].degree--;
      }
      total--;
    }
  }
  cout << ans << endl;
  return 0;
}
