#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const long long mod = 1e9 + 7;
const int N = 7e3 + 10;
int n;
struct node {
  long long x, y;
} a[N];
bool cmp(node a, node b) { return a.x < b.x; }
int vis[61];
int gg[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i].x;
  for (int i = 1; i <= n; i++) cin >> a[i].y;
  sort(a + 1, a + 1 + n, cmp);
  long long sum = 0;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    int j = i;
    while (j <= n && a[j].x == a[i].x) j++;
    if (j - i >= 2) {
      long long ans = 0;
      for (int k = i; k < j; k++) ans += a[k].y, gg[k] = 1;
      long long p = a[i].x;
      memset(vis, 0, sizeof vis);
      for (int k = 0; k <= 60; k++) {
        if ((p >> k) & 1) vis[k] = 1;
      }
      for (int k = 1; k < i; k++) {
        int flag = 0;
        for (int kk = 0; kk <= 60; kk++) {
          if (((a[k].x >> kk) & 1) && vis[kk] == 0) {
            flag = 1;
            break;
          }
        }
        if (!flag) {
          ans += a[k].y;
          gg[k] = 1;
        } else if (gg[k])
          ans += a[k].y;
      }
      sum = max(sum, ans);
      i = j - 1;
    }
  }
  cout << sum << endl;
  return 0;
}
