#include <bits/stdc++.h>
using namespace std;
const int mod1 = 1e9 + 7, mod2 = 998244353;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
const int N = 2e5 + 5;
struct node {
  long long a, b;
  bool operator<(const node &c) { return a > c.a; }
} arr[7005];
map<long long, int> mp;
set<int> st;
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i].a;
    mp[arr[i].a]++;
  }
  for (int i = 1; i <= n; i++) cin >> arr[i].b;
  long long ans = 0;
  for (auto &p : mp) {
    if (p.second <= 1) continue;
    for (int i = 1; i <= n; i++) {
      if ((arr[i].a & p.first) == arr[i].a) st.insert(i);
    }
  }
  for (auto &p : st) ans += arr[p].b;
  cout << ans << endl;
  return 0;
}
