#include <bits/stdc++.h>
using namespace std;
vector<int> e[20000];
int len, n, flag[20000], ou[20000];
long long a[20000], b[20000], ans;
void deal() {
  stack<int> s;
  int num = n, m1;
  while (1) {
    for (int i = 1; i <= n; i++) {
      if (!flag[i] && ou[i] == num - 1) s.push(i);
    }
    if (s.empty()) break;
    while (!s.empty()) {
      m1 = s.top();
      s.pop();
      num--;
      for (auto j : e[m1]) {
        ou[j]--;
      }
      flag[m1] = 1;
      ans -= b[m1];
    }
  }
  cout << ans;
}
int main() {
  long long m1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i], ans += b[i];
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      m1 = a[i] & a[j];
      if (a[i] ^ m1) {
        e[j].push_back(i);
        ou[i]++;
      }
      if (a[j] ^ m1) {
        e[i].push_back(j);
        ou[j]++;
      }
    }
  }
  deal();
}
