#include <bits/stdc++.h>
using namespace std;
map<long long int, int> mp;
bool c[7005] = {0};
int main() {
  int n;
  scanf("%d", &n);
  vector<long long int> a(n), b(n);
  for (int i = 0; i <= n - 1; ++i) {
    scanf("%lld", &a[i]);
    mp[a[i]]++;
  }
  long long int ans = 0;
  for (int i = 0; i <= n - 1; ++i) {
    scanf("%lld", &b[i]);
  }
  for (int i = 0; i < n; ++i) {
    if (mp[a[i]] > 1) {
      ans += b[i];
      c[i] = 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (mp[a[i]] == 1) {
      for (int j = 0; j < n; ++j) {
        if (((a[j] & a[i]) == a[i]) && c[j]) {
          ans += b[i];
          break;
        }
      }
    }
  }
  printf("%lld\n", ans);
}
