#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
stack<long long> s;
vector<long long> G[maxn];
struct node {
  long long num, val;
} a[maxn];
map<long long, long long> mp;
int vis[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].num);
    mp[a[i].num]++;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i].val);
  }
  long long ans = 0;
  for (auto now : mp) {
    if ((now.second) >= 2) {
      for (int i = 1; i <= n; i++) {
        if ((now.first | a[i].num) == now.first) {
          if (!vis[i]) ans = ans + a[i].val, vis[i] = 1;
        }
      }
    }
  }
  cout << ans << endl;
}
