#include <bits/stdc++.h>
using namespace std;
const int N = 1e4;
int n, b[N];
map<long long, long long> mp, mp2;
long long ans, a[N];
vector<long long> g;
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    mp[a[i]]++;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    mp2[a[i]] += b[i];
  }
  for (auto to : mp) {
    if (to.second > 1) {
      g.push_back(to.first);
      ans += mp2[to.first];
    }
  }
  reverse(g.begin(), g.end());
  for (auto to : mp) {
    if (to.second > 1) continue;
    bool ok = 0;
    for (auto to2 : g) {
      if (to2 < to.first) break;
      if ((to2 | to.first) == to2) {
        ok = 1;
        break;
      }
    }
    ans += mp2[to.first] * ok;
  }
  cout << ans;
  return 0;
}
