#include <bits/stdc++.h>
using namespace std;
int g[7001][7001] = {0};
int cnt[7001] = {0};
bool vis[7001] = {false};
int n, m;
int getm() {
  m = -1;
  int ans = INT_MIN;
  for (int i = 0; i < n; i++) {
    if (!vis[i] && cnt[i] > ans) {
      ans = cnt[i];
      m = i;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  int b[n];
  for (int i = 0; i < n; i++) cin >> b[i];
  long long z = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i] == a[j]) continue;
      for (int k = 0; k < 60; k++) {
        if ((a[i] & (z << k)) != 0 && (a[j] & (z << k)) == 0) {
          g[i][j] = 1;
          cnt[i]++;
          break;
        }
      }
    }
  }
  int size = n;
  while (getm() >= size - 1) {
    vis[m] = true;
    size--;
    for (int i = 0; i < n; i++)
      if (g[i][m] == 1) cnt[i]--;
  }
  if (size < 2) {
    cout << 0 << endl;
    return 0;
  }
  long long sum = 0;
  for (int i = 0; i < n; i++)
    if (!vis[i]) sum += b[i];
  cout << sum << endl;
  return 0;
}
