#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, i, j, k;
  long long hi;
  vector<long long> a, b, mul;
  vector<bool> group;
  vector<pair<long long, long long> > C;
  scanf("%d", &N);
  a.resize(N + 1);
  b.resize(N + 1);
  C.resize(N + 1);
  map<long long, int> M;
  for (i = 1; i <= N; ++i) {
    scanf("%lld", &a[i]);
    C[i].first = a[i];
    ++M[a[i]];
  }
  for (i = 1; i <= N; ++i) {
    scanf("%lld", &b[i]);
    C[i].second = b[i];
  }
  sort(C.begin() + 1, C.end());
  vector<long long> f;
  f.resize(N + 1);
  mul.resize(N + 1);
  for (i = 1; i <= N; ++i) mul[i] = M[C[i].first];
  group.resize(N + 1);
  for (i = 1; i <= N; ++i) {
    if (mul[i] > 1)
      group[i] = true;
    else
      group[i] = false;
  }
  for (i = 1; i <= N; ++i) {
    for (j = 1; j < i; ++j)
      if ((C[j].first & C[i].first) == C[j].first && group[i]) group[j] = true;
  }
  hi = 0;
  for (i = 1; i <= N; ++i)
    if (group[i]) hi += C[i].second;
  printf("%lld\n", hi);
  return 0;
}
