#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<long long> a(n), b(n), deg(n);
  vector<vector<int>> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  v.resize(n), deg.resize(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j || a[i] == a[j]) continue;
      for (long long k = 60; k >= 0; --k) {
        if ((a[i] & (1LL << k)) && (!(a[j] & (1LL << k)))) {
          v[j].push_back(i);
          deg[i]++;
          break;
        }
      }
    }
  }
  a.clear();
  int cur = n - 1, tcur = n - 1;
  bool once = true;
  vector<bool> can(n, true);
  while (once) {
    once = false;
    for (int j = 0; j < n; ++j) {
      if (can[j] && deg[j] == cur) {
        once = true;
        can[j] = false;
        tcur--;
        for (int k : v[j]) {
          if (can[k]) deg[k]--;
        }
      }
    }
    cur = tcur;
  }
  long long su = 0;
  for (int i = 0; i < n; ++i) {
    if (can[i]) su += b[i];
  }
  cout << su;
  return 0;
}
