#include <bits/stdc++.h>
using namespace std;
long long int ar[7005];
long long int b[7005];
long long int vis[7005];
map<long long int, long long int> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (ar[i] == ar[j]) {
        vis[i] = true;
        vis[j] = true;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      for (int j = 1; j <= n; j++) {
        if (vis[j] and (ar[i] & ar[j]) == ar[i]) vis[i] = true;
      }
    }
  }
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i]) ans += b[i];
  }
  cout << ans << endl;
  return 0;
}
