#include <bits/stdc++.h>
using namespace std;
const int maxn = 7e3 + 10;
long long a[maxn], b[maxn];
map<long long, long long> msk;
map<long long, long long> msk3;
map<long long, long long> msk2;
vector<long long> vec;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    msk[a[i]]++;
    if (msk[a[i]] >= 2 && !msk2[a[i]]) {
      vec.emplace_back(a[i]);
      msk2[a[i]]++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (auto it : vec) {
    for (int i = 0; i < n; i++) {
      if ((a[i] | it) == it && !msk3[i]) {
        msk3[i]++;
        ans += b[i];
      }
    }
  }
  cout << ans;
  return 0;
}
