#include <bits/stdc++.h>
using namespace std;
const int N = 7001;
long long a[N];
int b[N];
int bit[N][62];
int op[N], vis[N];
vector<int> v[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  long long res = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  if (n < 2) {
    printf("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int tmp = 1;
    long long t = a[i];
    while (t) {
      if (t & 1) bit[i][tmp] = 1;
      t /= 2;
      tmp++;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      int c1 = 0, c2 = 0;
      for (int k = 1; k <= 61; k++)
        if (bit[i][k] && !bit[j][k])
          c1 = 1;
        else if (!bit[i][k] && bit[j][k])
          c2 = 1;
      if (c1) {
        op[i]++;
        v[j].push_back(i);
      }
      if (c2) {
        op[j]++;
        v[i].push_back(j);
      }
    }
  int sv = n - 1;
  while (1) {
    int flag = 0;
    int tmp = 0;
    for (int i = 1; i <= n; i++)
      if (op[i] == sv) {
        vis[i] = 1;
        flag = 1;
      }
    for (int i = 1; i <= n; i++)
      if (vis[i] == 1) {
        tmp++;
        vis[i] = 2;
        for (int j = 0; j < v[i].size(); j++) op[v[i][j]]--;
      }
    sv -= tmp;
    if (!sv || !flag) break;
  }
  if (!sv) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) res += b[i];
  printf("%lld\n", res);
  return 0;
}
