#include <bits/stdc++.h>
using namespace std;
bool fun(vector<string> &v, string s) {
  bool f = false;
  for (auto i : v) {
    long long j = 0;
    for (; j < 60; j++) {
      if (s[j] == '1' && i[j] == '0') break;
    }
    if (j == 60) {
      f = true;
      break;
    }
  }
  if (f) {
    v.push_back(s);
    return true;
  }
  return false;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  vector<long long> a(n);
  vector<pair<long long, string>> v(n);
  map<long long, long long> u;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    u[a[i]]++;
  }
  vector<string> t;
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> v[i].first;
    v[i].second = bitset<60>(a[i]).to_string();
    if (u[a[i]] > 1) {
      t.push_back(v[i].second);
      sum += v[i].first;
    }
  }
  for (long long i = 0; i < n; i++) {
    if (u[a[i]] > 1) continue;
    if (fun(t, v[i].second)) sum += v[i].first;
  }
  cout << sum;
  return 0;
}
