#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16000000")
using namespace std;
long long mod = 998244353;
long double inf = 1e9;
long double eps = 1e-8;
long double pi = acos(-1);
ifstream in("input.txt");
ofstream out("output.txt");
vector<vector<int>> g;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<pair<long long, long long>> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i].second;
  }
  sort(a.begin(), a.end());
  vector<pair<int, pair<long long, long long>>> comp;
  comp.push_back({0, {a[0].first, a[0].second}});
  for (int i = 1; i < n; i++) {
    if (a[i].first == comp.back().second.first) {
      comp.back().first = 1;
      comp.back().second.second += a[i].second;
    } else {
      comp.push_back({0, {a[i].first, a[i].second}});
    }
  }
  int m = comp.size();
  for (int i = 0; i < m; i++) {
    for (int j = i + 1; j < m; j++) {
      if (comp[j].first == 1) {
        if ((comp[i].second.first & comp[j].second.first) ==
            comp[i].second.first) {
          comp[j].second.second += comp[i].second.second;
          comp[i].second.second = 0;
          break;
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < comp.size(); i++) {
    if (comp[i].first == 1) {
      ans += comp[i].second.second;
    }
  }
  cout << ans;
  return 0;
}
