#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 7000;
int n;
long long a[N_MAX + 2];
int b[N_MAX + 2];
map<long long, int> freq;
vector<int> sol;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) freq[a[i]]++;
  for (int i = 1; i <= n; i++)
    if (freq[a[i]] > 1) sol.push_back(i);
  for (int i = 1; i <= n; i++)
    if (freq[a[i]] == 1) {
      bool ok = false;
      for (int j : sol)
        if ((a[i] & a[j]) == a[i]) {
          ok = true;
          break;
        }
      if (ok == true) sol.push_back(i);
    }
  long long sum = 0;
  for (int i : sol) sum += b[i];
  cout << sum << "\n";
  return 0;
}
