#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const double EPS = 1e-6;
const int N = 1e6 + 5;
const long long mod = 1e9 + 7;
int oo = 1e9;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
vector<vector<int> > g;
pair<long long, long long> t[N];
int n;
map<long long, int> f;
int main() {
  scanf("%d", &n);
  g.resize(n + 1);
  for (int i = 0; i <= n - 1; i++) {
    scanf("%lld", &t[i].first);
    f[t[i].first]++;
  }
  for (int i = 0; i <= n - 1; i++) scanf("%lld", &t[i].second);
  sort(t, t + n);
  reverse(t, t + n);
  long long ans = 0;
  vector<long long> v;
  for (int i = 0; i <= n - 1; i++) {
    if (f[t[i].first] >= 2) {
      ans += t[i].second;
      v.push_back(t[i].first);
    }
  }
  for (int i = 0; i <= n - 1; i++) {
    if (f[t[i].first] >= 2) continue;
    for (int j = 0; j < v.size(); j++) {
      if ((t[i].first & v[j]) == t[i].first) {
        ans += t[i].second;
        v.push_back(t[i].first);
        break;
      }
    }
  }
  cout << ans;
}
