#include <bits/stdc++.h>
using namespace std;
string check(long long n) {
  long long binaryNum[60];
  long long i = 0;
  while (n > 0) {
    binaryNum[i] = n % 2;
    n = n / 2;
    i++;
  }
  string s;
  for (long long j = i - 1; j >= 0; j--) s.push_back(binaryNum[j]);
  return s;
}
signed main() {
  long long n;
  cin >> n;
  map<long long, pair<long long, long long>> m;
  long long a;
  vector<long long> v[62];
  long long A[n];
  long long B[n];
  for (long long i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (long long i = 0; i < n; i++) {
    cin >> B[i];
    m[A[i]].first += 1;
    m[A[i]].second += B[i];
  }
  vector<long long> vv;
  for (auto i = m.begin(); i != m.end(); i++) {
    if (i->second.first >= 2) vv.push_back(i->first);
  }
  long long ans = 0;
  long long t = 0;
  for (auto i = vv.begin(); i != vv.end(); i++) {
    a = (*i);
    for (long long j = 0; j < n; j++) {
      if ((A[j] | a) <= a) {
        t += B[j];
        B[j] = 0;
      }
    }
  }
  cout << t << endl;
}
