#include <bits/stdc++.h>
template <class T>
void UPRT(const T _) {
  if (_ >= 10) UPRT(_ / 10);
  putchar(_ % 10 + 48);
}
template <class T>
void PRT(const T _) {
  if (_ < 0) {
    putchar(45), UPRT<unsigned long long>(-(unsigned long long)_);
    return;
  }
  if (_ >= 10) PRT(_ / 10);
  putchar(_ % 10 + 48);
}
using std::bitset;
using std::cin;
using std::cout;
using std::deque;
using std::map;
using std::pair;
using std::priority_queue;
using std::set;
using std::sort;
using std::string;
using std::unique;
using std::unordered_map;
using std::unordered_set;
using std::vector;
struct Node {
  long long a, b;
  inline bool operator<(const Node &_o) const { return a < _o.a; }
};
int main() {
  int n;
  {
    char _c = getchar(), _v = 1;
    for (n = 0; _c < 48 || _c > 57; _c = getchar())
      if (_c == 45) _v = -1;
    for (; _c >= 48 && _c <= 57;
         n = (n << 1) + (n << 3) + _c - 48, _c = getchar())
      ;
    if (_v == -1) n = -n;
  }
  vector<Node> tp(n);
  for (auto i = 0; i < (n); ++i) {
    char _c = getchar(), _v = 1;
    for (tp[i].a = 0; _c < 48 || _c > 57; _c = getchar())
      if (_c == 45) _v = -1;
    for (; _c >= 48 && _c <= 57;
         tp[i].a = (tp[i].a << 1) + (tp[i].a << 3) + _c - 48, _c = getchar())
      ;
    if (_v == -1) tp[i].a = -tp[i].a;
  }
  for (auto i = 0; i < (n); ++i) {
    char _c = getchar(), _v = 1;
    for (tp[i].b = 0; _c < 48 || _c > 57; _c = getchar())
      if (_c == 45) _v = -1;
    for (; _c >= 48 && _c <= 57;
         tp[i].b = (tp[i].b << 1) + (tp[i].b << 3) + _c - 48, _c = getchar())
      ;
    if (_v == -1) tp[i].b = -tp[i].b;
  }
  std::sort((tp).begin(), (tp).end());
  unordered_map<long long, int> count;
  for (auto &e : tp) ++count[e.a];
  vector<Node> uni;
  unordered_set<long long> chosen;
  long long ans = 0;
  for (auto &e : tp)
    if (count[e.a] > 1)
      chosen.insert(e.a), ans += e.b;
    else
      uni.emplace_back(e);
  for (auto &e : uni)
    for (auto val : chosen)
      if ((val & e.a) == e.a && (chosen.insert(e.a), ans += e.b)) break;
  PRT(ans), putchar(10);
  return 0;
}
