#include <bits/stdc++.h>
using namespace std;
long long n;
long long* a;
long long* b;
map<long long, long long> mp;
long long sum = 0;
vector<long long> v;
int main() {
  scanf("%lld", &n);
  a = new long long[n];
  b = new long long[n];
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    mp[a[i]]++;
  }
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &b[i]);
    if (mp[a[i]] > 1 || mp[a[i]] == -1) {
      sum += b[i];
      if (mp[a[i]] > 1) {
        v.push_back(a[i]);
        mp[a[i]] = -1;
      }
    }
  }
  for (long long i = 0; i < n; i++) {
    if (mp[a[i]] == 1) {
      for (long long j = 0; j < v.size(); j++) {
        long long k = a[i] | v[j];
        if (k == v[j]) {
          v.push_back(a[i]);
          sum += b[i];
          break;
        }
      }
    }
  }
  cout << sum;
  return 0;
}
