#include <bits/stdc++.h>
using namespace std;
int n, color;
vector<int> topsort;
vector<int> was;
bool g[7001][7001];
bool gt[7001][7001];
bool gg[7001][7001];
vector<long long> dp;
vector<int> sm;
vector<int> marked;
void dfs(int v) {
  was[v] = true;
  for (int to = 0; to < n; ++to) {
    if (g[v][to] && !was[to]) {
      dfs(to);
    }
  }
  topsort.push_back(v);
}
void dfs2(int v, int color) {
  was[v] = color;
  for (int to = 0; to < n; ++to) {
    if (gt[v][to] && !was[to]) {
      dfs2(to, color);
    }
  }
}
void f(int v) {
  if (marked[v]) {
    return;
  }
  marked[v] = 1;
  for (int to = 0; to < color; ++to) {
    if (gg[v][to]) {
      f(to);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<long long> b(n);
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  was.resize(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j) {
        continue;
      }
      if ((a[i] & a[j]) == a[j]) {
        g[i][j] = 1;
        gt[j][i] = 1;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!was[i]) {
      dfs(i);
    }
  }
  reverse(topsort.begin(), topsort.end());
  fill(was.begin(), was.end(), 0);
  for (int i = 0; i < n; ++i) {
    int v = topsort[i];
    if (!was[v]) {
      dfs2(v, ++color);
    }
  }
  vector<long long> cost(color);
  sm.resize(color);
  for (int i = 0; i < n; ++i) {
    cost[was[i] - 1] += b[i];
    sm[was[i] - 1]++;
  }
  for (int v = 0; v < n; ++v) {
    for (int to = 0; to < n; ++to) {
      if (g[v][to] && was[v] != was[to]) {
        int v1 = was[v] - 1;
        int v2 = was[to] - 1;
        gg[v1][v2] = 1;
      }
    }
  }
  marked.resize(color);
  long long res = 0;
  for (int i = 0; i < color; ++i) {
    if (sm[i] > 1) {
      f(i);
    }
  }
  for (int i = 0; i < color; ++i) {
    if (marked[i]) {
      res += cost[i];
    }
  }
  cout << res << endl;
  return 0;
}
