#include <bits/stdc++.h>
using namespace std;
bool used[100];
string f(long long n) {
  string s;
  while (n) {
    s += char('0' + (n % 2));
    n /= 2;
  }
  return s;
}
int main() {
  int n;
  cin >> n;
  map<long long, int> m;
  long long a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]]++;
  }
  int b[n];
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  vector<string> v;
  map<long long, bool> used;
  long long res = 0;
  bool in = false;
  for (int i = 0; i < n; ++i) {
    if (m[a[i]] > 1) {
      in = true;
      res += b[i];
      string s = f(a[i]);
      if (!used[a[i]]) {
        v.push_back(s);
        used[a[i]] = true;
      }
      for (int j = 0; s[j]; ++j) {
        if (s[j] == '1') {
          used[j] = true;
        }
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (m[a[i]] == 1) {
      if (a[i] == 0) {
        if (in) {
          res += b[i];
        }
      } else {
        string s = f(a[i]);
        bool add = false;
        for (int j = 0; j < v.size(); ++j) {
          bool Add = true;
          if (v[j].size() >= s.size()) {
            for (int k = 0; s[k]; ++k) {
              if (s[k] == '1' && v[j][k] != '1') {
                Add = false;
                break;
              }
            }
            if (Add) {
              add = true;
              break;
            }
          }
        }
        if (add) {
          res += b[i];
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
