#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long MAX = 2000000000;
const double ERROR = 0.0000001;
const long long N_6 = 1000000 + 10;
const long long N_5 = 100000 + 10;
const long long N_4 = 10000 + 10;
const long long N_3 = 1000 + 10;
const long long N_2 = 100 + 10;
const long long LEN = 31;
long long n;
long long a[N_4];
long long b[N_4];
bool adj_matrix[N_4][N_4];
bool visited[N_4];
void dfs(long long curr) {
  visited[curr] = true;
  for (long long i = 0; i < n; ++i) {
    if (adj_matrix[curr][i] and not visited[i]) {
      dfs(i);
    }
  }
}
int main() {
  long long T = 1;
  for (long long t = 1; t <= T; t++) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (long long i = 0; i < n; ++i) {
      cin >> a[i];
    }
    for (long long i = 0; i < n; ++i) {
      cin >> b[i];
    }
    set<int> start;
    for (long long i = 0; i < n; ++i) {
      for (long long j = 0; j < n; ++j) {
        adj_matrix[i][j] = ((a[i] & a[j]) == a[j]);
        if (i > j and adj_matrix[i][j] and adj_matrix[j][i]) {
          start.insert(i);
        }
      }
    }
    for (long long i : start) {
      dfs(i);
    }
    long long ans = 0;
    for (long long i = 0; i < n; ++i) {
      ans += visited[i] * b[i];
    }
    cout << ans << endl;
  }
  return 0;
}
