#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 6;
const long long MOD = 1e9 + 7;
long long a[7006], b[7006], nisi[7006];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  map<long long, int> cnt;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cnt[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  vector<long long> v;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (cnt[a[i]] > 1) {
      v.push_back(a[i]);
      ans += b[i];
      nisi[i] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (nisi[i]) continue;
    for (long long x : v) {
      if ((x | a[i]) == x) {
        ans += b[i];
        break;
      }
    }
  }
  cout << ans;
}
