#include <bits/stdc++.h>
using namespace std;
const int N = 7010;
map<long long, int> cnt;
int n;
long long ar[N], br[N];
bitset<N> bit[N];
bitset<N> res;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    cnt[ar[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> br[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if ((ar[i] | ar[j]) == ar[i]) {
        bit[i][j] = 1;
      } else {
        bit[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (cnt[ar[i]] >= 2) {
      res = (res | bit[i]);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (res[i]) {
      ans += br[i];
    }
  }
  cout << ans << '\n';
  return 0;
}
