#include <bits/stdc++.h>
using namespace std;
long long a[300005], b[300005];
vector<long long> v;
bitset<62> bit[300005], mx, xx, y;
bool vis[300005];
int main() {
  long long ans = 0;
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    for (long long j = 0; j <= 60; j++) {
      if ((a[i] >> j) & 1) bit[i][j] = 1;
    }
  }
  for (long long i = 1; i <= n; i++) scanf("%lld", &b[i]);
  bool f = 0;
  for (long long i = 1; i <= n; i++) {
    if (vis[i]) continue;
    long long cnt = 0;
    long long now = b[i];
    for (long long j = 1; j <= n; j++) {
      if (j == i) continue;
      if (a[j] == a[i]) {
        vis[j] = 1;
        cnt++, now += b[j];
      }
    }
    if (cnt > 0) {
      vis[i] = 1;
      ans += now;
      mx |= bit[i];
      f = 1;
      v.push_back(i);
    }
  }
  if (f) {
    for (long long i = 1; i <= n; i++) {
      if (vis[i]) continue;
      bool ff = 0;
      for (auto x : v) {
        xx = bit[i] | bit[x];
        if ((int)xx.count() <= (int)bit[x].count()) {
          ff = 1;
        }
      }
      if (ff) ans += b[i];
    }
  }
  cout << ans << endl;
}
