#include <bits/stdc++.h>
using namespace std;
const long long N = 7005;
long long n, ans = 0;
long long a[N], b[N], mark[N];
map<long long, long long> f;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    f[a[i]]++;
  }
  for (long long i = 1; i <= n; i++) cin >> b[i];
  for (long long i = 1; i <= n; i++) {
    if (f[a[i]] > 1) mark[i] = 1;
    if (!mark[i]) continue;
    for (long long j = 1; j <= n; j++)
      if ((a[i] & a[j]) == a[j]) mark[j] = 1;
  }
  for (long long i = 1; i <= n; i++)
    if (mark[i]) ans += b[i];
  cout << ans;
  return 0;
}
