#include <bits/stdc++.h>
using namespace std;
int n;
bool dd[7005];
pair<long long, int> a[7005];
long long ans;
int main() {
  cin >> n;
  memset(a, -1, sizeof a);
  for (int i = 1; i <= n; i++) cin >> a[i].first;
  for (int i = 1; i <= n; i++) cin >> a[i].second;
  sort(a + 1, a + n + 1, greater<pair<long long, int>>());
  for (int i = 1; i <= n; i++)
    if (a[i].first == a[i + 1].first)
      for (int j = i; j <= n; j++) {
        long long cur = a[i].first & a[j].first;
        if ((cur == a[j].first) && !dd[j]) {
          ans += a[j].second;
          dd[j] = 1;
        }
      }
  cout << ans;
  return 0;
}
