#include <bits/stdc++.h>
using namespace std;
bool mark[60], bin[3502][60];
pair<long long, long long> ara[7002];
int main() {
  long long n, i, j, k, ind, ans;
  bool fg;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) scanf("%lld", &ara[i].first);
  for (i = 1; i <= n; i++) scanf("%lld", &ara[i].second);
  sort(ara + 1, ara + n + 1);
  ans = 0, ind = -1;
  for (i = n; i > 0; i--) {
    fg = false;
    memset(mark, false, sizeof(mark));
    if (i > 1 && ara[i].first == ara[i - 1].first) {
      while (i > 1 && ara[i].first == ara[i - 1].first)
        ans += ara[i].second, i--;
      ans += ara[i].second;
      fg = true, ind++;
    }
    for (j = 0; j < 60; j++) {
      if ((1LL << j) & ara[i].first) {
        mark[j] = true;
        if (fg) bin[ind][j] = true;
      }
    }
    if (!fg) {
      for (j = 0; j <= ind; j++) {
        for (k = 0; k < 60; k++) {
          if (mark[k] && !bin[j][k]) break;
        }
        if (k == 60) {
          ans += ara[i].second;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
