#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
using namespace std;
pair<long long int, long long int> ar[7001];
bool comp(pair<long long int, long long int> a,
          pair<long long int, long long int> b) {
  return a.first > b.first;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> ar[i].first;
  for (int i = 1; i <= n; i++) cin >> ar[i].second;
  sort(ar + 1, ar + n + 1, comp);
  long long int res = 0;
  int cnt = 0;
  vector<int> vis(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    int j = i;
    while (j <= n && ar[j].first == ar[i].first) {
      cnt++, j++;
      if (cnt == 2)
        vis[i] = vis[i + 1] = 1;
      else if (cnt > 2)
        vis[j - 1] = 1;
    }
    if (cnt == 1) continue;
    while (j <= n) {
      if (((ar[j].first ^ ar[i].first) & ar[j].first) == 0) vis[j] = 1;
      j++;
    }
  }
  for (int i = 1; i <= n; i++)
    if (vis[i]) res += ar[i].second;
  cout << res;
}
