#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int m) {
  long long int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int> > v;
  long long int a[n + 2], b[n + 2];
  bool done[n + 2];
  memset(done, 0, sizeof(done));
  for (long long int i = 1; i < n + 1; i++) cin >> a[i];
  for (long long int i = 1; i < n + 1; i++) {
    long long int x;
    cin >> x;
    v.push_back(make_pair(a[i], x));
  }
  sort((v).begin(), (v).end());
  long long int ans = 0;
  long long int cnt = 0;
  for (long long int i = v.size() - 1; i >= 0; i--) {
    if (i && (v[i].first == v[i - 1].first))
      done[i] = 1;
    else {
      for (long long int j = i + 1; j < v.size(); j++) {
        if (done[j] && ((v[i].first & v[j].first) == v[i].first)) done[i] = 1;
      }
    }
    if (done[i]) {
      cnt++;
      ans += v[i].second;
    }
  }
  if (cnt >= 2)
    cout << ans << endl;
  else
    cout << 0 << endl;
}
