#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  long long a, b;
  cin >> n;
  vector<pair<long long, long long>> vp(n);
  vector<int> rem(n);
  for (int i = 0; i < n; i++) cin >> vp[i].first;
  for (int i = 0; i < n; i++) cin >> vp[i].second;
  sort(vp.begin(), vp.end());
  reverse(vp.begin(), vp.end());
  long long sol = 0;
  for (int i = 0; i < n; i++) {
    bool check = false;
    for (int j = 0; j < n; j++) {
      if (i == j || rem[j]) continue;
      if ((vp[i].first | vp[j].first) == vp[j].first) {
        check = true;
        break;
      }
    }
    if (check)
      sol += vp[i].second;
    else
      rem[i] = 1;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (rem[i] == 0) cnt++;
  if (cnt == 0) sol = 0;
  cout << sol << "\n";
  return 0;
}
