#include <bits/stdc++.h>
using namespace ::std;
pair<long long int, long long int> ar[7005];
bool m[7005];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ar[i].first;
  for (int i = 0; i < n; i++) cin >> ar[i].second;
  sort(ar, ar + n);
  long long int ans = 0;
  vector<long long int> v;
  for (int i = 0; i < n - 1; i++)
    if (ar[i].first == ar[i + 1].first) {
      v.push_back(ar[i].first);
      int j = i + 1;
      ans += ar[i].second;
      m[i] = 1;
      while (j < n && ar[j].first == ar[j - 1].first)
        ans += ar[j].second, m[j] = 1, j++;
    }
  for (int i = n - 1; i > -1; i--)
    if (!m[i]) {
      bool did = 0;
      for (auto a : v)
        if (!(ar[i].first & (ar[i].first ^ a))) {
          did = 1;
          break;
        }
      if (did) ans += ar[i].second, m[i] = 1;
    }
  ans = 0;
  for (int i = 0; i < n; i++)
    if (m[i]) ans += ar[i].second;
  cout << ans << '\n';
}
