#include <bits/stdc++.h>
using namespace std;
const int N = 7070;
pair<long long, int> a[N];
int n;
int main() {
  scanf("%d", &n);
  for (int i = (0); i < (n); i++) scanf("%lld", &a[i].first);
  for (int i = (0); i < (n); i++) scanf("%d", &a[i].second);
  sort(a, a + n);
  long long ans = 0;
  vector<long long> c;
  vector<pair<long long, int> > b;
  for (int l = 0, r = 0; l < n; l = r) {
    while (r < n && a[r].first == a[l].first) r++;
    if (r - l <= 1) {
      b.push_back(a[l]);
      continue;
    }
    for (int i = (l); i < (r); i++) ans += a[i].second;
    c.push_back(a[l].first);
  }
  for (int i = (0); i < ((int)b.size()); i++) {
    for (int j = (0); j < ((int)c.size()); j++) {
      if ((b[i].first & c[j]) - b[i].first == 0) {
        ans += b[i].second;
        break;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
