#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f;
const long long MAXN = 0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
void file() {}
const long long N = 1e4 + 5;
long long a[N], b[N], vis[N];
multiset<long long> mul;
signed main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  file();
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i], mul.insert(a[i]);
  for (long long i = 0; i < n; i++) cin >> b[i];
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    if (mul.count(a[i]) > 1 && vis[i] == 0) {
      for (long long j = 0; j < n; j++) {
        if ((a[i] | a[j]) == a[i] && vis[j] == 0) {
          ans += b[j];
          vis[j] = 1;
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
