#include <bits/stdc++.h>
using namespace std;
long long int input(long long int n, vector<long long int> &ve);
long long int input(long long int n, vector<long long int> &ve) {
  long long int val;
  for (long long int i = 0; i < n; i++) {
    cin >> val;
    ve.push_back(val);
  }
  return 0;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, a, check = 0;
  vector<long long int> v, v1, bl;
  vector<pair<long long int, long long int>> vc;
  set<long long int> st;
  cin >> n;
  bl.resize(n + 2, 0);
  input(n, v);
  input(n, v1);
  for (long long int i = 0; i < n; i++) {
    vc.push_back(make_pair(v[i], v1[i]));
  }
  sort(vc.begin(), vc.end());
  for (long long int i = 0; i < n - 1; i++) {
    if (vc[i].first == vc[i + 1].first) {
      check = check | (vc[i].first);
      bl[i] = 1;
      bl[i + 1] = 1;
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (bl[i] == 0) {
      long long int flag = 0;
      for (int j = 0; j < n; j++) {
        if (bl[j] == 1) {
          long long int t = vc[j].first | vc[i].first;
          if (t == vc[j].first) {
            flag = 1;
            break;
          }
        }
      }
      if (flag == 1) {
        bl[i] = 1;
      }
    }
  }
  long long int sum = 0, cnt = 0;
  for (long long int i = 0; i < n; i++) {
    if (bl[i] == 1) {
      ++cnt;
    }
    sum += (vc[i].second) * bl[i];
  }
  if (cnt < 2) {
    sum = 0;
  }
  cout << sum << endl;
  return 0;
}
