#include <bits/stdc++.h>
using namespace std;
const int N = 500023;
vector<int> adj[N];
bool vis[N];
set<int> s;
void solve() {
  long long int n;
  cin >> n;
  long long int ans = 0;
  map<long long int, long long int> cnt;
  vector<long long int> a;
  for (long long int i = 0; i < n; i++) {
    long long int num;
    cin >> num;
    a.push_back(num);
  }
  vector<long long int> b;
  for (long long int i = 0; i < n; i++) {
    long long int num;
    cin >> num;
    b.push_back(num);
  }
  for (long long int i = 0; i < n; i++) {
    cnt[a[i]]++;
  }
  for (long long int i = 0; i < n; i++) {
    if (cnt[a[i]] >= 2) {
      vis[i] = 1;
      ans += b[i];
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (vis[i]) continue;
    for (long long int j = 0; j < n; j++) {
      if (vis[j] && ((a[j] | a[i]) == a[j])) {
        ans += b[i];
        break;
      }
    }
  }
  cout << ans;
}
int main() {
  long long int T;
  T = 1;
  long long int t = 0;
  while ((t++) < T) {
    solve();
    cout << endl;
  }
  return 0;
}
