#include <bits/stdc++.h>
using namespace std;
multiset<long long> pp;
long long a[7005], b[7005];
int vis[7005];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pp.insert(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  long long ans = 0;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    ans = 0;
    if (pp.count(a[i]) > 1) {
      for (int j = 1; j <= n; j++) {
        if (((a[j] | a[i]) == a[i]) && !vis[j]) {
          ans += b[j];
          vis[j] = 1;
        }
      }
      sum += ans;
    }
  }
  cout << sum << endl;
  return 0;
}
