#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  vector<pair<long long, int>> vct;
  scanf("%d", &n);
  vct.resize(n);
  for (i = 0; i < n; i++) cin >> vct[i].first;
  for (i = 0; i < n; i++) cin >> vct[i].second;
  vector<int> bl(n, 0);
  sort(vct.begin(), vct.end(), greater<pair<long long, int>>());
  long long ans = 0;
  for (i = 0; i < n - 1; i++) {
    if (bl[i]) continue;
    if (vct[i].first == vct[i + 1].first) {
      ans += vct[i].second;
      bl[i] = 1;
      for (j = i + 1; j < n; j++) {
        if (!((vct[i].first ^ vct[j].first) & (~vct[i].first))) {
          if (!bl[j]) {
            bl[j] = 1;
            ans += vct[j].second;
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
