#include <bits/stdc++.h>
using namespace std;
long long dx[] = {-1, 1, 0, 0};
long long dy[] = {0, 0, -1, 1};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<bool> ok(n, false);
  vector<long long> b(n), a(n);
  long long sum = 0;
  map<long long, long long> m;
  for (long long i = 0; i < n; ++i) {
    long long p;
    cin >> p;
    a[i] = p;
    m[p]++;
  }
  for (auto &k : b) cin >> k;
  long long num = 0;
  for (long long i = 0; i < n; ++i) {
    if (m[a[i]] > 1) {
      sum += b[i];
      continue;
    } else {
      long long x = 0, z = 0;
      for (auto &k : m) {
        if (k.second > 1) {
          x++;
          long long y = (k.first | a[i]);
          if (y != k.first) z++;
        }
      }
      if (x == z or x == 0) {
      } else
        sum += b[i];
    }
  }
  cout << sum;
}
