#include <bits/stdc++.h>
using namespace std;
struct stud {
  long long int id;
  long long int a;
  long long int b;
  long long int cnt;
};
bool cmp(stud* a, stud* b) { return a->cnt < b->cnt; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vector<stud*> a(n);
  vector<stud*> aa(n);
  for (int i = 0; i < n; i++) {
    a[i] = new stud();
    a[i]->id = i;
    cin >> a[i]->a;
    aa[i] = a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i]->b;
    a[i]->cnt = 0;
  }
  long long int x;
  vector<vector<bool>> st(n, vector<bool>(n, false));
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      x = __builtin_popcountll(a[i]->a & a[j]->a);
      if (__builtin_popcountll(a[i]->a) > x) {
        st[i][j] = true;
        a[i]->cnt += 1;
      }
      if (__builtin_popcountll(a[j]->a) > x) {
        st[j][i] = true;
        a[j]->cnt += 1;
      }
    }
  }
  int cnt = a.size();
  while (cnt > 1) {
    sort(a.begin(), a.begin() + cnt, cmp);
    if (a[cnt - 1]->cnt < cnt - 1) {
      break;
    }
    cnt--;
    int remove = a[cnt]->id;
    for (int i = 0; i < n; i++) {
      if (st[i][remove]) {
        aa[i]->cnt -= 1;
      }
    }
  }
  if (cnt == 1) {
    cout << "0\n";
    return 0;
  }
  long long int res = 0;
  for (int i = 0; i < cnt; i++) {
    res += a[i]->b;
  }
  cout << res << "\n";
  return 0;
}
