#include <bits/stdc++.h>
using namespace std;
long long a[7010];
int b[7010];
bool Map[7010][7010];
int sz[7010];
queue<int> W;
long long ans;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), ans += b[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      if ((a[i] & a[j]) == a[i]) Map[i][j] = 1, sz[i]++;
    }
  }
  for (int i = 1; i <= n; i++)
    if (!sz[i]) W.push(i);
  while (!W.empty()) {
    int x = W.front();
    W.pop();
    ans -= b[x];
    for (int i = 1; i <= n; i++) {
      if (Map[i][x]) {
        if (--sz[i] == 0) W.push(i);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
