#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, vis[maxn];
struct Node {
  long long a, b;
  bool operator<(Node x) const { return a < x.a; }
} S[maxn];
map<long long, int> mp;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> S[i].a, mp[S[i].a]++;
  for (int i = 1; i <= n; i++) cin >> S[i].b;
  sort(S + 1, S + n + 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (mp[S[i].a] > 1) {
      int st = i;
      long long v = S[st].a;
      ans += S[i].b;
      vis[st] = 1;
      while (i < n && S[i + 1].a == v) {
        i++;
        ans += S[i].b;
        vis[i] = 1;
      }
      for (int j = st - 1; j >= 1; j--) {
        if (!vis[j] && (S[j].a | v) == v) ans += S[j].b, vis[j] = 1;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
