#include <bits/stdc++.h>
const int maxn = 1e4 + 5;
using namespace std;
long long a[maxn], b[maxn];
map<long long, int> mp;
vector<int> vec;
map<int, bool> vis;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    mp[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (mp[a[i]] >= 2) {
      vec.push_back(i);
      vis[i] = 1;
    }
  }
  vector<int>::iterator it;
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    for (it = vec.begin(); it != vec.end(); it++) {
      int x = *it;
      if (a[i] < a[x] && ((a[i] & a[x]) == a[i])) {
        vec.push_back(i);
        vis[i] = 1;
        break;
      }
    }
  }
  for (auto x : vec) ans += b[x];
  printf("%lld\n", ans);
  return 0;
}
