#include <bits/stdc++.h>
using namespace std;
unsigned long long read() {
  unsigned long long ret = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ret = (ret << 1) + (ret << 3) + c - 48;
    c = getchar();
  }
  return ret * f;
}
const int maxn = 1e4;
unsigned long long a[maxn], b[maxn];
unsigned long long cnt[maxn];
int n;
bool ch(int i) {
  for (int j = 0; j < n; j++) {
    if (cnt[j] > 1 && (a[j] | a[i]) == a[j]) return 1;
  }
  return 0;
}
int main() {
  n = read();
  for (int i = 0; i < n; i++) a[i] = read();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (a[i] == a[j]) cnt[i]++;
    }
  for (int i = 0; i < n; i++) b[i] = read();
  unsigned long long ans1 = 0;
  for (int i = 0; i < n; i++) {
    if (cnt[i] == 1 && !ch(i)) b[i] = 0;
    ans1 += b[i];
  }
  cout << ans1;
}
