#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > v(n);
  for (long long i = 0; i < (long long)n; i++) cin >> v[i].first;
  for (long long i = 0; i < (long long)n; i++) cin >> v[i].second;
  sort(v.begin(), v.end());
  map<long long, long long> mp;
  for (long long i = 0; i < (long long)n; i++) mp[v[i].first]++;
  vector<long long> key;
  for (auto i = mp.begin(); i != mp.end(); i++)
    if (i->second > 1) key.emplace_back(i->first);
  long long ans = 0;
  for (long long i = 0; i < (long long)n; i++) {
    long long flag = 0;
    for (long long j = 0; j < (long long)key.size(); j++) {
      if ((key[j] | v[i].first) == key[j]) flag = 1;
    }
    if (flag == 1) ans += v[i].second;
  }
  cout << ans << endl;
}
