#include <bits/stdc++.h>
using namespace std;
long long A[7011];
long long B[7011];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> A[i];
  for (int i = 0; i < n; i++) cin >> B[i];
  map<long long, long long> m;
  vector<long long> v;
  map<long long, int> cnt;
  for (int i = 0; i < n; i++) {
    long long a = A[i], b = B[i];
    m[a] += b;
    int c = ++cnt[a];
    if (c == 2) v.push_back(a);
  }
  long long sum = 0;
  for (auto& it : v) sum += m[it];
  for (auto& it : cnt) {
    if (it.second == 1) {
      long long a = it.first, b = m[it.first];
      for (auto& vit : v) {
        if (((vit ^ a) & a) == 0) {
          sum += b;
          break;
        }
      }
    }
  }
  cout << sum;
  return 0;
}
