#include <bits/stdc++.h>
using namespace std;
long long d1[8000];
long long d2[8000];
long long judge[8000];
vector<long long> v;
bool cmp(long long p, long long q) {
  long long k1 = p;
  long long k2 = q;
  while (k1 != 0) {
    if (((k2 & 1) == 1) && ((k1 & 1) != 1)) return false;
    k1 = k1 >> 1;
    k2 = k2 >> 1;
  }
  if (k2 != 0) return false;
  return true;
}
signed main() {
  long long n;
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &d1[i]);
  }
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &d2[i]);
  }
  long long sum = 0;
  long long s;
  for (long long i = 1; i <= n; i++) {
    long long flag = 0;
    if (judge[i] == 1) continue;
    for (long long j = i + 1; j <= n; j++) {
      if (d1[i] == d1[j]) {
        if (flag == 0) {
          sum = sum + d2[i];
          flag = 1;
          judge[i] = 1;
          v.push_back(d1[i]);
        }
        judge[j] = 1;
        sum = sum + d2[j];
      }
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (judge[i] == 1) continue;
    for (long long j = 0; j < v.size(); j++) {
      if (cmp(v[j], d1[i]) == true) {
        sum = sum + d2[i];
        break;
      }
    }
  }
  cout << sum << endl;
}
