#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool sortinrev(const pair<int, int>& a, const pair<int, int>& b) {
  if (a.first == b.first) return b.second > a.second;
  return a.first > b.first;
}
void run(vector<int> v) {
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
int modInverse(long long int a, long long int m) {
  long long int m0 = m;
  long long int y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long int q = a / m;
    long long int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
void numberOfNodes(int n, vector<long long int> adj[],
                   vector<long long int>& count1, int s, int e) {
  vector<long long int>::iterator u;
  count1[s] = 1;
  for (u = adj[s].begin(); u != adj[s].end(); u++) {
    if (*u == e) continue;
    numberOfNodes(n, adj, count1, *u, s);
    count1[s] += count1[*u];
  }
}
void solve() {
  int n;
  cin >> n;
  long long int a[n], b[n];
  int vis[n];
  long long int ans = 0;
  vector<vector<int>> v, total;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  for (int i = 0; i < n; i++) {
    int count = 0;
    if (!vis[i]) {
      for (int j = 0; j < n; j++) {
        if (j != i) {
          if (a[i] == a[j]) {
            if (count == 0) ans += b[i];
            vis[j] = 1;
            vis[i] = 1;
            ans += b[j];
            count++;
          }
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    vector<int> vi(61);
    long long int m = a[i];
    int count = 0;
    vector<int> ind;
    while (m > 0) {
      if (m % 2 == 1) {
        ind.push_back(count);
      }
      m = m / 2;
      count++;
    }
    for (int j = 0; j < ind.size(); j++) vi[ind[j]] = 1;
    total.push_back(vi);
    if (vis[i]) v.push_back(vi);
  }
  int count1, k;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      for (int j = 0; j < v.size(); j++) {
        for (k = 0; k < 60; k++) {
          if (total[i][k] == 1) {
            if (v[j][k] == 0) break;
          }
        }
        if (k == 60) {
          ans += b[i];
          vis[i] = 1;
          v.push_back(total[i]);
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
