#include <bits/stdc++.h>
using namespace std;
const int maxn = 7005;
unsigned long long a[maxn];
unsigned long long b[maxn];
int vis[maxn];
map<unsigned long long, int> mm;
vector<unsigned long long> v;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%llu", &a[i]);
    mm[a[i]]++;
  }
  for (int i = 0; i < n; i++) scanf("%llu", &b[i]);
  set<unsigned long long> S;
  for (map<unsigned long long, int>::iterator it = mm.begin(); it != mm.end();
       it++) {
    if ((*it).second > 1) {
      for (int i = 0; i < n; i++) {
        if (((*it).first | a[i]) == (*it).first) S.insert(i);
      }
    }
  }
  unsigned long long ans = 0;
  for (set<unsigned long long>::iterator it = S.begin(); it != S.end(); it++)
    ans += b[*it];
  printf("%llu\n", ans);
  return 0;
}
