#include <bits/stdc++.h>
using namespace std;
const int maxn = 7e3 + 5;
typedef struct {
  long long a, b;
} STU;
STU stu[maxn];
int vis[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%lld", &stu[i].a);
  for (int i = 1; i <= n; ++i) scanf("%lld", &stu[i].b);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (vis[i] != 0) continue;
    int fe = 0;
    for (int j = 1; j <= n; ++j)
      if (i != j && stu[i].a == stu[j].a) {
        fe = 1;
        break;
      }
    if (!fe) continue;
    long long tmp = stu[i].b;
    vis[i] = 1;
    for (int j = 1; j <= n; ++j) {
      if (i == j || vis[j] != 0) continue;
      if ((stu[j].a | stu[i].a) == stu[i].a) {
        tmp += stu[j].b;
        vis[j] = 1;
      }
    }
    ans += tmp;
  }
  printf("%lld", ans);
  return 0;
}
