#include <bits/stdc++.h>
using namespace std;
const long long N = 7000;
long long n;
long long a[N], b[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (long long i = 0; i < n; ++i) cin >> a[i];
  for (long long i = 0; i < n; ++i) cin >> b[i];
  unordered_map<long long, long long> f, g;
  for (long long i = 0; i < n; ++i) {
    f[a[i]]++;
    g[a[i]] += b[i];
  }
  long long ans = 0;
  vector<long long> dups;
  for (auto p : f) {
    if (p.second > 1) {
      dups.push_back(p.first);
      ans += g[p.first];
    }
  }
  for (auto p : f) {
    if (p.second == 1) {
      for (auto dup : dups) {
        if ((p.first & dup) == p.first) {
          ans += g[p.first];
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
