#include <bits/stdc++.h>
using namespace std;
struct __timestamper {};
long long solve(vector<long long> _a, vector<long long> _b) {
  vector<pair<long long, long long>> a;
  for (int i = 0; i < (int)_a.size(); i++) {
    a.push_back({_a[i], _b[i]});
  }
  int n = a.size();
  sort(a.begin(), a.end());
  vector<int> deg(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((a[i].first | a[j].first) != a[j].first) {
        deg[i]++;
      }
    }
  }
  for (int taken = n; taken >= 1; taken--) {
    int cur = -1;
    for (int v = 0; v < n; v++) {
      if (deg[v] == taken - 1) {
        cur = v;
        break;
      }
    }
    if (cur == -1) break;
    deg[cur] = -1;
    for (int j = 0; j < n; j++) {
      if (deg[j] >= 0 && (a[j].first | a[cur].first) != a[cur].first) {
        deg[j]--;
      }
    }
  }
  long long sum = 0;
  for (int i = 0; i < n; i++)
    if (deg[i] >= 0) sum += a[i].second;
  return sum;
}
int main() {
  int n;
  while (cin >> n) {
    vector<long long> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    cout << solve(a, b) << endl;
  }
  return 0;
}
