#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, batas, sz, ans = 0;
  pair<long long int, long long int> p[7005];
  vector<long long int> v;
  scanf("%lld", &n);
  for (long long int i = 0; i < n; i++) scanf("%lld", &p[i].first);
  for (long long int i = 0; i < n; i++) scanf("%lld", &p[i].second);
  sort(p, p + n);
  for (long long int i = 0; i < n; i++) {
    batas = i;
    while (batas + 1 < n) {
      if (p[batas + 1].first != p[i].first) break;
      batas++;
    }
    if (batas > i) v.push_back(p[i].first);
    i = batas;
  }
  sz = v.size();
  for (long long int i = 0; i < n; i++)
    for (long long int j = 0; j < sz; j++)
      if ((p[i].first & v[j]) == p[i].first) {
        ans += p[i].second;
        break;
      }
  printf("%lld\n", ans);
  return 0;
}
