#include <bits/stdc++.h>
using namespace std;
struct QualChar {
  QualChar() : count(0), cost(0){};
  QualChar(const QualChar& qc) {
    count = qc.count;
    cost = qc.cost;
  };
  int count;
  int64_t cost;
};
bool fold(int64_t msk1, int64_t msk2) {
  bool greater1 = false;
  for (int bit = 0; bit < 60; ++bit) {
    if (msk2 % 2 && !(msk1 % 2)) {
      greater1 = true;
    }
    if (msk1 % 2 && !(msk2 % 2)) {
      return false;
    }
    msk1 >>= 1;
    msk2 >>= 1;
  }
  return greater1;
}
int main() {
  int N;
  cin >> N;
  map<int64_t, QualChar> mask;
  vector<int64_t> inputs(N);
  for (int i = 0; i < N; ++i) {
    int64_t msk;
    cin >> msk;
    inputs[i] = msk;
  }
  for (int i = 0; i < N; ++i) {
    int64_t cost;
    cin >> cost;
    mask[inputs[i]].count++;
    mask[inputs[i]].cost += cost;
  }
  vector<int64_t> moreone;
  int64_t best_sum = 0;
  for (auto& pr : mask) {
    if (pr.second.count > 1) {
      moreone.push_back(pr.first);
      best_sum += pr.second.cost;
    }
  }
  for (auto& pr : mask) {
    for (auto group : moreone) {
      if (fold(pr.first, group) && pr.second.count < 2) {
        best_sum += pr.second.cost;
        break;
      }
    }
  }
  cout << best_sum;
  return 0;
}
