#include <bits/stdc++.h>
using namespace std;
const int MAXN = 7000;
pair<long long, long long> S[MAXN + 5];
deque<pair<long long, long long>> q;
vector<long long> v;
map<long long, int> Size;
bool used[MAXN + 5];
int main() {
  int n = 0, cnt = 1;
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &S[i].first);
    ++Size[S[i].first];
  }
  for (int i = 1; i <= n; ++i) scanf("%lld", &S[i].second);
  S[n + 1].first = -1000;
  int cnt1 = 1;
  for (int i = 1; i <= n; ++i) {
    if (Size[S[i].first] == 1)
      q.push_back(S[i]);
    else if (Size[S[i].first] == 0) {
      ans += S[i].second;
    } else {
      Size[S[i].first] = 0;
      ans += S[i].second;
      v.push_back(S[i].first);
    }
  }
  deque<pair<long long, long long>>::iterator it = q.begin(), t;
  for (int i = 0; i < v.size(); ++i) {
    for (it = q.begin(); it != q.end(); ++it) {
      if ((v[i] & (v[i] ^ (*it).first)) == (v[i] ^ (*it).first)) {
        ans += (*it).second;
        (*it).first = ((long long)1 << 61);
      }
    }
  }
  printf("%lld\n", ans);
}
