#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n), b(n);
  for (long long& x : a) cin >> x;
  for (long long& x : b) cin >> x;
  vector<bool> ok(n);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (ok[i]) continue;
    int c = 0;
    for (int j = 0; j < n; j++)
      if (a[i] == a[j]) c++;
    if (c < 2) continue;
    for (int j = 0; j < n; j++)
      if (!ok[j]) {
        long long more = a[j] & ~a[i];
        if (!more) {
          ans += b[j];
          ok[j] = 1;
        }
      }
  }
  cout << ans << '\n';
}
