#include <bits/stdc++.h>
using namespace std;
const int N = 7e3;
long long n;
long long a[N], b[N];
map<long long, int> freq;
bool vis[N];
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    freq[a[i]]++;
  }
  for (int i = 0; i < n; i++) cin >> b[i];
  vector<long long> v;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (freq[a[i]] > 1) v.push_back(a[i]), ans += b[i], vis[i] = true;
  }
  for (int i = 0; i < n; i++) {
    if (vis[i]) continue;
    bool f = 0;
    for (auto j : v) {
      if ((j & a[i]) == a[i]) f = 1;
    }
    if (f) ans += b[i];
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  solve();
  return 0;
}
