#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long long MX = 1e9;
const long long INF = 1e9;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  pair<long long, long long> p[n];
  for (int i = 0; i < n; ++i) cin >> p[i].first;
  for (int i = 0; i < n; ++i) cin >> p[i].second;
  sort(p, p + n);
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (i && p[i].first == p[i - 1].first) continue;
    if (i + 1 < n && p[i].first == p[i + 1].first) {
      for (int j = 0; j < n; ++j) {
        long long xr = p[i].first ^ p[j].first;
        if ((p[i].first | xr) == p[i].first) {
          ans += p[j].second;
          p[j].second = 0;
        }
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
