#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 1;
int MOD = 1e9 + 7;
const int INF = 1e9 + 1;
const long long LINF = 1e18;
bool sub(int x, int y) {
  for (int i = 0; i < 60; ++i) {
    if (((1 << i) & y) == 0 && ((1 << i) & x) != 0) return false;
  }
  return true;
}
void solve() {
  int n;
  cin >> n;
  map<long long, long long> s;
  vector<long long> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    s[a[i]]++;
  }
  vector<long long> g;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    if (s[a[i]] > 1) {
      ans += b[i];
      g.push_back(a[i]);
    }
  }
  for (int i = 0; i < n; ++i) {
    if (s[a[i]] == 1) {
      for (int j = 0; j < g.size(); ++j) {
        if ((a[i] & g[j]) == a[i]) {
          ans += b[i];
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  for (int i = 0; i < t; ++i) {
    solve();
  }
}
