#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1e9;
const long long LINF = (long long)3e18;
const double EPS = 1e-7;
const int MAX = 20 * 1000 + 47;
const int MAS = 4 * 1e5 + 10;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const int LOG = 19;
const int ar = 1e5 + 100;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<long long> a(n);
  vector<int> b(n);
  long long ans = 0;
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
  }
  for (int i = (0); i < (n); i++) cin >> b[i];
  vector<int> used(n, 0);
  for (int i = (0); i < (n); i++) {
    if (used[i]) continue;
    int cnt = 0;
    for (int j = (0); j < (n); j++)
      if (a[j] == a[i]) cnt++;
    if (cnt >= 2) {
      for (int j = (0); j < (n); j++) {
        if (!used[j] && (a[i] & a[j]) == a[j]) {
          used[j] = 1;
          ans += b[j];
        }
      }
    }
  }
  cout << ans << '\n';
}
