#include <bits/stdc++.h>
using namespace std;
long long n, a[10000], b[10000], has;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] == a[j]) {
        has += b[i];
        b[i] = 0;
        for (int k = 1; k <= n; k++) {
          if ((a[i] & a[k]) == a[k]) {
            has += b[k];
            b[k] = 0;
          }
        }
        break;
      }
    }
  }
  cout << has << endl;
}
