#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long a[n], b[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) cin >> b[i];
  map<long long, vector<long long>> mp;
  vector<long long> v, g;
  for (long long i = 0; i < n; i++) mp[a[i]].push_back(i);
  for (auto x : mp) {
    if ((long long)x.second.size() > 1) {
      v.push_back(x.first);
      for (long long u : x.second) {
        ans += b[u];
      }
    } else
      g.push_back(x.second[0]);
  }
  for (long long u : g) {
    for (long long x : v) {
      if ((a[u] & x) == a[u]) {
        ans += b[u];
        v.push_back(a[u]);
        break;
      }
    }
  }
  cout << ans << endl;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
}
