#include <bits/stdc++.h>
using namespace std;
long long foo[7000 + 1], item[7000 + 1], val[7000 + 1];
void solve() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> item[i];
  for (long long i = 1; i <= n; i++) cin >> val[i];
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      if (i == j) continue;
      if ((item[i] & item[j]) == item[i]) foo[i]++;
    }
  }
  bool b[n + 1][n + 1];
  memset(b, false, sizeof(b));
  while (1) {
    long long f = 0;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        if (i == j) continue;
        if ((item[i] & item[j]) == item[i] && foo[j] == 0 && b[i][j] == false) {
          b[i][j] = true;
          foo[i]--;
          f = 1;
        }
      }
    }
    if (!f) break;
  }
  long long c = 0, ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (foo[i] > 0) {
      c++;
      ans += val[i];
    }
  }
  if (c > 1)
    cout << ans;
  else
    cout << 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  while (t--) solve();
}
