#include <bits/stdc++.h>
using namespace std;
struct Student {
  static const int len = 60;
  bool a[len];
  unsigned long long b;
  Student(unsigned long long aa = 0, unsigned long long bb = 0) : b{bb} {
    for (int i = 0; i < len; ++i) {
      a[i] = (aa % 2);
      aa /= 2;
    }
  }
  bool operator>(Student &other) {
    for (int i = len; i > 0; --i) {
      if (a[i - 1] != other.a[i - 1]) return (a[i - 1]);
    }
    return (b > other.b);
  }
  bool operator==(Student &other) {
    for (int i = len; i > 0; --i) {
      if (a[i - 1] != other.a[i - 1]) return false;
    }
    return true;
  }
  bool majorize(Student &other) {
    for (int i = len; i > 0; --i) {
      if (!a[i - 1] && other.a[i - 1]) return false;
    }
    return true;
  }
};
static bool order(Student &it, Student &other) { return (it > other); }
int main() {
  int n;
  cin >> n;
  vector<Student> students(n);
  for (int i = 0; i < n; ++i) {
    unsigned long long a;
    cin >> a;
    students[i] = Student(a);
  }
  for (int i = 0; i < n; ++i) {
    unsigned long long b;
    cin >> b;
    students[i].b = b;
  }
  sort(students.begin(), students.end(), order);
  unsigned long long sum = 0;
  while (students.size() > 1) {
    if (!(students[0] == students[1])) {
      students.erase(students.begin());
    } else {
      Student captain = students[0];
      auto underling = students.begin();
      while (underling != students.end()) {
        if (captain.majorize(*underling)) {
          sum += underling->b;
          students.erase(underling);
        } else {
          ++underling;
        }
      }
    }
  }
  cout << sum << endl;
}
