#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
long long arr1[N];
long long arr2[N];
long long vis[N];
map<long long, long long> cnt;
vector<long long> arr;
int main() {
  long long n, l;
  long long ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", arr1 + i);
    cnt[arr1[i]]++;
  }
  for (int i = 0; i < n; i++) {
    scanf("%I64d", arr2 + i);
    if (cnt[arr1[i]] >= 2) {
      vis[i] = 1;
      arr.push_back(arr1[i]);
      ans = ans + arr2[i];
    }
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      l = arr.size();
      for (int j = 0; j < l; j++) {
        if (arr1[i] < arr[j] && ((arr1[i] & arr[j]) == arr1[i])) {
          arr.push_back(arr1[i]);
          ans = ans + arr2[i];
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
