#include <bits/stdc++.h>
using namespace std;
const int maxx = 100005;
const int inf = 2000000000;
const long long infll = 1000000000000000000LL;
const int lg = 17;
const long long mod = 1000000007;
int n;
int deg[maxx], st[maxx];
long long arr[maxx], brr[maxx];
vector<int> v[maxx];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 1; i <= n; i++) cin >> brr[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (arr[i] & (~arr[j])) {
        deg[i]++;
        v[j].push_back(i);
      }
    }
  }
  int cur = n;
  long long res = 0;
  bool f = true;
  while (f) {
    for (int i = 1; i <= n; i++) {
      if (!st[i] && deg[i] == cur - 1) {
        for (int j : v[i]) {
          deg[j]--;
        }
        cur--;
        st[i] = 1;
        break;
      }
      if (i == n) f = false;
    }
  }
  for (int i = 1; i <= n; i++) res += (!st[i]) * brr[i];
  cout << res << endl;
}
