#include <bits/stdc++.h>
using namespace std;
long long P = 1e9 + 7;
long long mul(long long a, long long b) {
  long long ans = 0;
  for (; b; a = a * 2 % P, b >>= 1)
    if (b & 1) ans = (ans + a) % P;
  return ans;
}
long long qpow(long long a, long long n) {
  long long r = 1;
  for (; n; a = a * a, n >>= 1)
    if (n & 1) r = r * a;
  return r;
}
long long inv(long long x) { return x <= 1 ? 1 : inv(P % x) * (P - P / x) % P; }
const int maxn = 7e3 + 5;
int deg[maxn];
bool cmp(int x, int y) { return deg[x] > deg[y]; }
struct Solution {
  int n;
  long long a[maxn], b[maxn];
  int p[maxn];
  void Prepare() {}
  bool Init() {
    if (scanf("%d", &n) == 1) {
      for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
      for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
      return 1;
    }
    return 0;
  }
  void Solve() {
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) {
        if (a[i] == a[j]) continue;
        long long x = a[i] & a[j];
        if (x != a[i] && x != a[j]) {
          deg[i]++;
          deg[j]++;
        } else if (x == a[i])
          deg[j]++;
        else
          deg[i]++;
      }
    for (int i = 1; i <= n; i++) p[i] = i;
    sort(p + 1, p + n + 1, cmp);
    int num = n - 1;
    for (int i = 1; i <= n; i++) {
      int x = p[i];
      if (deg[x] == num) {
        b[x] = 0;
        for (int j = 1; j <= n; j++) {
          if (b[j] == 0)
            continue;
          else {
            if (a[x] == a[j]) continue;
            long long t = a[x] & a[j];
            if (t != a[x] && t != a[j])
              deg[x]--, deg[j]--;
            else if (t == a[x])
              deg[j]--;
            else
              deg[x]--;
          }
        }
        num--;
      }
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans += b[i];
    cout << ans << "\n";
  }
} H;
signed main() {
  H.Prepare();
  {
    H.Init();
    H.Solve();
  }
}
