#include <bits/stdc++.h>
using namespace std;
const long long maxinput = 7005;
long long a[maxinput], b[maxinput];
set<long long> myset;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long temp[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    temp[i] = a[i];
  }
  for (long long i = 0; i < n; i++) cin >> b[i];
  sort(temp, temp + n);
  for (long long i = n - 2; i >= 0; i--) {
    if (temp[i] == temp[i + 1]) {
      for (long long j = 0; j < n; j++) {
        if ((a[j] & temp[i]) == a[j]) myset.insert(j);
      }
    }
  }
  long long ans = 0;
  for (auto itr = myset.begin(); itr != myset.end(); itr++) {
    ans += b[(*itr)];
  }
  cout << ans << endl;
}
