#include <bits/stdc++.h>
using namespace std;
const long long int INFL = 1e18;
const int INFI = 1e9;
long long int div_floor(const long long int &a, const long long int &b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
long long int div_ceil(const long long int &a, const long long int &b) {
  return a / b + (((a ^ b) >= 0) and a % b);
}
long long int modpow(long long int a, long long int b,
                     long long int m = 1000000007) {
  a %= m;
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
long long int bpow(long long int a, long long int b) {
  long long int res = 1;
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
long long int modinv(long long int a, long long int m = 1000000007) {
  return modpow(a, m - 2, m);
}
void graph(vector<vector<int> > &adj, int m) {
  int x, y;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    adj[--x].push_back(--y);
    adj[y].push_back(x);
  }
}
void solve() {
  int n;
  cin >> n;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> b(n);
  for (int i = 0; i < n; i++) cin >> b[i];
  vector<bool> taken(n, 0);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (a[i] == a[j] && j != i) taken[i] = taken[j] = 1;
  for (int i = 0; i < n; i++)
    if (!taken[i])
      for (int j = 0; j < n; j++)
        if (taken[j] && ((a[i] | a[j]) == a[j])) taken[i] = true;
  long long int sum = 0;
  long long int cnt = 0;
  for (int i = 0; i < n; i++)
    if (taken[i]) cnt++, sum += b[i];
  if (cnt < 2) sum = 0;
  cout << sum << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) solve();
}
