#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long inf = 1e18;
long long vis[maxn];
struct node {
  long long a, b;
} d[maxn];
bool cmp(node a, node b) { return a.a < b.a; }
void solve() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> d[i].a;
  for (long long i = 1; i <= n; i++) cin >> d[i].b;
  sort(d + 1, d + 1 + n, cmp);
  long long res = 0;
  for (long long i = 2; i <= n; i++) {
    if (d[i].a == d[i - 1].a) {
      vis[i] = vis[i - 1] = 1;
      for (long long j = 1; j < i; j++) {
        if ((d[i].a & d[j].a) == d[j].a) vis[j] = 1;
      }
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (vis[i]) res += d[i].b;
  }
  cout << res;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
