#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > a(n);
  for (long long i = 0; i < n; i++) cin >> a[i].first;
  for (long long i = 0; i < n; i++) cin >> a[i].second;
  sort(a.begin(), a.end());
  long long ans = 0;
  vector<long long> temp;
  for (long long i = 0; i < n; i++) {
    long long x = a[i].first;
    if (i < n - 1 && a[i + 1].first == x) {
      while (i < n && a[i].first == x) ans += a[i].second, a[i].second = 0, i++;
      i--;
      temp.push_back(x);
    }
  }
  for (long long i = 0; i < n; i++) {
    if (a[i].second == 0) continue;
    bool ad = 0;
    for (long long j = 0; j < temp.size(); j++) {
      if ((a[i].first & temp[j]) == a[i].first) {
        ad = 1;
        break;
      }
    }
    if (ad) ans += a[i].second;
  }
  cout << ans;
}
