#include <bits/stdc++.h>
using namespace std;
long long a[7002], b[7002];
map<long long, long long> tk;
vector<long long> dis;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n;
  cin >> n;
  long long val = 0;
  vector<long long> yet;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    tk[a[i]]++;
    if (tk[a[i]] == 2) yet.push_back(a[i]);
  }
  for (long long i = 0; i < n; i++) cin >> b[i];
  long long ans = 0;
  long long ch = 0;
  for (long long i = 0; i < n; i++) {
    for (auto val : yet) {
      if ((val | a[i]) == val) {
        ans += b[i];
        ch++;
        break;
      }
    }
  }
  if (ch > 1)
    cout << ans;
  else
    cout << 0;
  return 0;
}
