#include <bits/stdc++.h>
using namespace std;
const int MAXN = 7001, INF = 1e9;
int n;
int used[MAXN], sz[MAXN];
long long c[MAXN];
vector<int> g[MAXN];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j) {
        continue;
      }
      if ((a[i] & a[j]) == a[i]) {
        g[j].push_back(i);
        ++sz[i];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (!sz[j] && !used[j]) {
        used[j] = 1;
        for (int &k : g[j]) {
          --sz[k];
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      ans += c[i];
    }
  }
  cout << ans;
  return 0;
}
