#include <bits/stdc++.h>
using namespace std;
bool is_greater(long long a, long long b) {
  long long temp = ((a | b) - (a & b));
  if ((temp - (temp & a)) == temp)
    return false;
  else
    return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  long long a[n], b[n];
  vector<int> zero_num;
  map<int, bool> included;
  int tot_count[n];
  bool **set;
  set = new bool *[n];
  for (int i = 0; i < n; i++) {
    set[i] = new bool[n];
    included[i] = true;
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    tot_count[i] = n - 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (is_greater(a[i], a[j])) {
        set[i][j] = true;
        tot_count[i]--;
      } else if (i == j) {
        set[i][j] = true;
      } else {
        set[i][j] = false;
      }
    }
    if (!tot_count[i]) {
      zero_num.push_back(i);
      included.erase(i);
    }
  }
  vector<int> rem_idx;
  while (zero_num.size()) {
    int idx = zero_num[(zero_num.size()) - 1];
    zero_num.pop_back();
    for (auto it = included.begin(); it != included.end(); it++) {
      if (set[it->first][idx] == false && tot_count[it->first]) {
        tot_count[it->first]--;
        if (tot_count[it->first] == 0) {
          zero_num.push_back(it->first);
          rem_idx.push_back(it->first);
        }
      }
    }
    for (int i = 0; i < rem_idx.size(); i++) {
      included.erase(rem_idx[i]);
    }
    rem_idx.clear();
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (tot_count[i]) {
      ans += b[i];
    }
  }
  cout << ans << endl;
  return 0;
}
