#include <bits/stdc++.h>
using namespace std;
const int MX = 7000 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int n;
long long a[MX];
int b[MX], vis[MX];
map<long long, int> mp;
vector<int> nxt[MX];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    mp[a[i]]++;
  }
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j || a[i] == a[j]) continue;
      if ((a[i] & a[j]) == a[j]) nxt[i].push_back(j);
    }
  }
  long long ans = 0;
  for (auto x : mp) {
    if (x.second < 2) continue;
    int id = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i] == x.first) {
        vis[i] = 1;
        id = i;
      }
    }
    for (auto v : nxt[id]) vis[v] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (vis[i]) ans += b[i];
  cout << ans << endl;
  return 0;
}
