#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<pair<long long int, long long int>> v;
  for (int i = 0; i < n; i++) {
    v.push_back(make_pair(a[i], b[i]));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  long long int ans = 0;
  {}
  for (int i = 0; i < n; i++) {
    if (v[i].first == v[i + 1].first && i + 1 < n) {
      long long int x = v[i - 1].first;
      vector<long long int> vect;
      while (i < n) {
        if (v[i].first == v[i + 1].first && i + 1 < n) {
          vect.push_back(v[i].first);
        }
        vector<long long int>::iterator it;
        int z = 0, flag = 0;
        for (it = vect.begin(); it < vect.end(); it++) {
          long long int y = v[i].first | vect[z];
          if (y == vect[z]) {
            flag = 1;
          }
          z++;
        }
        if (flag == 1) {
          ans += v[i].second;
        }
        i++;
      }
    }
  }
  cout << ans;
  return 0;
}
