#include <bits/stdc++.h>
using namespace std;
const int maxn = 7050;
pair<long long, long long> p[maxn];
map<long long, int> mp;
bool vis[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].first;
    mp[p[i].first]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> p[i].second;
  }
  memset(vis, 0, sizeof vis);
  long long ans = 0;
  for (auto it : mp) {
    if (it.second >= 2) {
      for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        if ((it.first | p[i].first) == it.first) {
          ans += p[i].second;
          vis[i] = true;
        }
      }
    }
  }
  cout << ans << endl;
}
