#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<ll>> g;
vector<pair<ll, ll>> v;
vector<pair<ll, ll>> used;
ll sum = 0;
bool curCicle = false;

void dfs(ll vert, ll stage)
{
  used[vert].first = stage;

  for (auto &i : g[vert])
  {
    if (i == stage)
    {
      curCicle = true;
    }
  }

  for (auto &i : g[vert])
  {
    if (used[i].first == -1)
    {
      dfs(i, stage);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  v.resize(n);

  for (auto &i : v) cin >> i.first;
  for (auto &i : v) cin >> i.second;

  for (ll i = 0; i < n; ++i)
  {
    for (ll j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto t1 = v[i].first;
      auto t2 = v[j].first;
      bool ok = true;
      while (t1 || t2)
      {
        if ((t1 % 2 == 1) && (t2 % 2 == 0)) ok = false;
        t1 /= 2;
        t2 /= 2;
      }
      if (ok)
      {
        g[j].push_back(i);
      }
    }
  }

  used.assign(n, { -1, 0 });
  for (ll i = 0; i < n; ++i)
  {
    ll ans = 0;
    dfs(i, i);

    if (!curCicle) continue;
    curCicle = false;

    for (ll j = 0; j < n; ++j)
    {
      if (used[j].first == i)
      {
        used[j].second = 1;
      }
    }
  }

  sum = 0;
  for (int i = 0; i < n; ++i)
  {
    if (used[i].second)
    {
      sum += v[i].second;
    }
  }
  cout << sum;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<set<ll>> g;
vector<char> used;
vector<ll> a;
vector<ll> cost;
map<ll, pair<ll, ll>> num;

ll answer = 0;

void dfs(int v)
{
  answer += cost[v];
  used[v] = true;

  for (auto &i : g[v])
  {
    if (!used[i])
    {
      dfs(i);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  used.assign(n, false);
  a.assign(n, 0);
  cost.assign(n, 0);

  for (int i = 0; i < n; ++i)
  {
    cin >> a[i];
    ++num[a[i]].first;
    num[a[i]].second = i;
  }

  for (auto &i : cost)
  {
    cin >> i;
  }

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto temp1 = a[i];
      auto temp2 = a[j];

      bool ok = true;

      while (temp1 || temp2)
      {
        if (temp2 % 2 == 1 && temp1 % 2 == 0)
        {
          ok = false;
        }

        temp1 /= 2;
        temp2 /= 2;
      }

      if (ok)
      {
        g[i].insert(j);
      }
    }
  }

  for (auto &i : num)
  {
    if (i.second.first <= 1) continue;
    if (!used[i.second.second]) dfs(i.second.second);
  }

  cout << answer;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

ll n, m;
vector<vector<ll>> g;
vector<pair<ll, ll>> v;
vector<pair<ll, ll>> used;
ll sum = 0;
bool curCicle = false;

void dfs(ll vert, ll stage)
{
  used[vert].first = stage;

  for (auto &i : g[vert])
  {
    if (i == stage)
    {
      curCicle = true;
    }
  }

  for (auto &i : g[vert])
  {
    if (used[i].first != stage)
    {
      dfs(i, stage);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  v.resize(n);

  for (auto &i : v) cin >> i.first;
  for (auto &i : v) cin >> i.second;

  for (ll i = 0; i < n; ++i)
  {
    for (ll j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto t1 = v[i].first;
      auto t2 = v[j].first;
      bool ok = true;
      while (t1 || t2)
      {
        if ((t1 % 2 == 1) && (t2 % 2 == 0)) ok = false;
        t1 /= 2;
        t2 /= 2;
      }
      if (ok)
      {
        g[j].push_back(i);
      }
    }
  }

  used.assign(n, { -1, 0 });
  for (ll i = 0; i < n; ++i)
  {
    ll ans = 0;
    dfs(i, i);

    if (!curCicle) continue;
    curCicle = false;

    for (ll j = 0; j < n; ++j)
    {
      if (used[j].first == i)
      {
        used[j].second = 1;
      }
    }
  }

  sum = 0;
  for (int i = 0; i < n; ++i)
  {
    if (used[i].second)
    {
      sum += v[i].second;
    }
  }
  cout << sum;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include<iostream>
#include<iterator>
#include<math.h>
#include<cmath>
#include<sstream>
#include<vector>
#include<iomanip>
#include<string>
#include<fstream>
#include<algorithm>
#include<map>
#include<thread>
#include<stack>
#include<numeric>
#include<functional>
#include<queue>
#include<set>
#include<list>
#include<bitset>

typedef long long ll;

using namespace std;

const ll MOD = (ll)1000000007;
const ll inf = (ll)9e18;
const long double eps = 0.0000001;

struct th
{
  ll first, second, w;
  ll counter;
};

int n, m;
vector<vector<int>> g;
vector<pair<ll, ll>> v;
vector<pair<ll, ll>> used;
ll sum = 0;
bool curCicle = false;

void dfs(ll vert, ll stage)
{
  used[vert].first = stage;

  for (auto &i : g[vert])
  {
    if (used[i].first == stage)
    {
      curCicle = true;
    }
  }

  for (auto &i : g[vert])
  {
    if (used[i].first != stage)
    {
      dfs(i, stage);
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(30);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  g.resize(n);
  v.resize(n);

  for (auto &i : v) cin >> i.first;
  for (auto &i : v) cin >> i.second;

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if (i == j) continue;

      auto t1 = v[i].first;
      auto t2 = v[j].first;
      bool ok = true;
      while (t1 || t2)
      {
        if ((t1 % 2) && (t2 % 2 == 0)) ok = false;
        t1 /= 2;
        t2 /= 2;
      }
      if (ok)
      {
        g[j].push_back(i);
      }
    }
  }

  used.assign(n, { -1, 0 });
  for (int i = 0; i < n; ++i)
  {
    ll ans = 0;
    dfs(i, i);

    if (!curCicle) continue;
    curCicle = false;

    for (int j = 0; j < n; ++j)
    {
      if (used[j].first == i)
      {
        ans += v[j].second;
      }
    }
    sum = max(ans, sum);
  }
  cout << sum;

  return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];
int cnt[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}

	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = 0; i < n; ++i) {
		cnt[i] = mp[v[i].first];
	}

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

	for(int i = n - 1; i >= 0; --i) {
		if(cnt[i] < 2) continue;
		for(int j = 0; j <= i; ++j) vis[j] = false;
		cur = 0;
		for(int j = i; j >= 0; --j) {
			if(!vis[j] and cnt[j] >= 2) {
				dfs(j);
			}
		}
		if(cur > mx) {
			mx = cur;
		}
	}
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}
	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

	for(int i = n - 1; i >= 0; --i) {
		if(mp[v[i].first] < 2) continue;
		for(int j = 0; j < n; ++j) vis[j] = false;
		cur = 0;
		for(int j = i; j >= 0; --j) {
			if(!vis[j] and mp[v[i].first] >= 2) {
				dfs(j);
			}
		}
		mx = max(mx, cur);
	}
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}
	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

	for(int i = n - 1; i >= 0; --i) {
		if(mp[v[i].first] < 2) continue;
		for(int j = 0; j < n; ++j) vis[j] = false;
		cur = 0;
		for(int j = i; j >- 0; --j) {
			if(!vis[j]) {
				dfs(j);
			}
		}
		mx = max(mx, cur);
	}
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];
int cnt[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}

	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = 0; i < n; ++i) {
		cnt[i] = mp[v[i].first];
	}

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

		for(int j = 0; j < n; ++j) vis[j] = false;
		cur = 0;
		for(int j = n - 1; j >= 0; --j) {
			if(!vis[j] and cnt[j] >= 2) {
				dfs(j);
			}
		}
		if(cur > mx) {
			mx = cur;
		}
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}
	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

	for(int i = n - 1; i >= 0; --i) {
		if(mp[v[i].first] < 2) continue;
		for(int j = 0; j < n; ++j) vis[j] = false;
		cur = 0;
		for(int j = i; j >= 0; --j) {
			if(!vis[j] and mp[v[j].first] >= 2) {
				dfs(j);
			}
		}
		mx = max(mx, cur);
	}
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}
	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

	for(int j = 0; j < n; ++j) vis[j] = false;
	cur = 0;
	for(int j = n - 1; j >- 0; --j) {
		if(!vis[j] && mp[v[j].first] >= 2) {
			dfs(j);
		}
	}
	mx = max(mx, cur);
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


#include "bits/stdc++.h"
using namespace std;

const int N = 7005;

int n , m;

map < long long , int > mp;

bool vis[N];

long long cur;

vector<pair<long long, int>> v;
vector<int> adj[N];

void dfs(int u) {
	vis[u] = true;
	cur += v[u].second;
	for(auto nxt : adj[u]) {
		if(!vis[nxt]) {
			dfs(nxt);
		}
	}
}

void solve() {
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	v.resize(n);
	for(int i = 0; i < n; ++i) {
		long long a;
		cin >> a;
		v[i].first = a;
		mp[a]++;
	}
	for(int i = 0; i < n; ++i) {
		int b;
		cin >> b;
		v[i].second = b;
	}
	sort(v.begin(), v.end());

	for(int i = n - 1; i >= 0; --i) {
		for(int j = i - 1; j >= 0; --j) {
			if((v[i].first & v[j].first) == v[j].first) {
				adj[i].push_back(j);
			}
		}
	}

	long long mx = 0;

	for(int i = n - 1; i >= 0; --i) {
		if(mp[v[i].first] < 2) continue;
		for(int j = 0; j < n; ++j) vis[j] = false;
		cur = 0;
		for(int j = i; j >= 0; --j) {
			if(!vis[j]) {
				dfs(j);
			}
		}
		mx = max(mx, cur);
	}
	cout << mx << endl;
}

int main() {
	solve();
	return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

long long a[N], b[N];
map<long long, int> mp;
map<long long, long long> mpb;

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    for (int i = 0; i < n; i++) {
        mp[a[i]]++;
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
            long long sum = b[i];
            bool is = 0;
            for (int j = 0; j < n; j++) {
                if(i == j) continue;
                if(a[i] == a[j]) is = 1;
                if ((a[j] | a[i]) <= a[i]) {
                    sum += b[j];
                    // cerr << a[i] << " :" << a[j] << "\n";
                }
            }
            if (is) ans = max(ans, sum);
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

unsigned long long a[N], b[N];

bool mark[N];
int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    unsigned long long ans = 0;

    for (int i = 0; i < n; i++) {
        if (mark[i]) continue;
        unsigned long long sum = b[i];
        bool is = 0;

        for (int j = 0; j < n; j++) {
            if (mark[j]) continue;
            if(i == j) continue;
            if(a[i] == a[j]) is = 1;
            if ((a[i] | a[j]) == a[i]) {
                sum += b[j];
            }
        }

        if (is) {
            ans += sum;
            mark[i] = 1;
            for (int j = 0; j < n; j++) {
                if (mark[j]) continue;
                if(i == j) continue;
                if(a[i] == a[j]) is = 1;
                if ((a[i] | a[j]) == a[i]) {
                    mark[j] = 1;
                }
            }
        }
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

vector<int> g[100];

long long a[N], b[N];
map<long long, int> mp;
map<long long, long long> mpb;

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    for (int i = 0; i < n; i++) {
        mp[a[i]]++;
        mpb[a[i]] += b[i];
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        if (mp[a[i]] > 1) {
            long long sum = 0;
            for (int j = 0; j < n; j++) {
                if (((a[i]^a[j]) | a[i]) > a[i]) {;}
                else sum += b[j];
            }
            ans = max(ans, sum);
        }
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

unsigned long long a[N], b[N];

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    unsigned long long ans = 0;

    for (int i = 0; i < n; i++) {
        unsigned long long sum = b[i];
        bool is = 0;

        for (int j = 0; j < n; j++) {
            if(i == j) continue;
            if(a[i] == a[j]) is = 1;
            if ((a[j] | a[i]) == a[i]) {
                sum += b[j];
            }
        }

        if (is) ans = max(ans, sum);
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

long long a[N], b[N];
map<long long, int> mp;
map<long long, long long> mpb;

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    for (int i = 0; i < n; i++) {
        mp[a[i]]++;
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        if (mp[a[i]] > 1) {
            long long sum = 0;
            for (int j = 0; j < n; j++) {
                if ((a[j] | a[i]) <= a[i]) sum += b[j];
            }
            ans = max(ans, sum);
        }
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

long long a[N], b[N];

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    long long ans = 0;

    for (int i = 0; i < n; i++) {
        long long sum = b[i];
        bool is = 0;

        for (int j = 0; j < n; j++) {
            if(i == j) continue;
            if(a[i] == a[j]) is = 1;
            if ((a[j] | a[i]) == a[i]) {
                sum += b[j];
            }
        }

        if (is) ans = max(ans, sum);
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

vector<int> g[100];

long long a[N], b[N];
map<long long, int> mp;
map<long long, long long> mpb;

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    for (int i = 0; i < n; i++) {
        mp[a[i]]++;
        mpb[a[i]] += b[i];
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        if (mp[a[i]] > 1) {
            long long sum = mpb[0];
            long long cp = a[i];
            for (long long x = cp; x; x = (x-1) & cp) {
                sum += mpb[x];
            }
            ans = max(ans, sum);
        }
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

long long a[N], b[N];
map<long long, int> mp;
map<long long, long long> mpb;

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    for (int i = 0; i < n; i++) {
        mp[a[i]]++;
    }

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        long long sum = b[i];
        long long x = 0;
        bool is = 0;
        for (int j = 0; j < n; j++) {
            if(i == j) continue;
            if ((a[j] | a[i]) <= a[i]) {
                sum += b[j];
                x |= a[j];
                // cerr << a[i] << " :" << a[j] << "\n";
            }
        }
        if (x == a[i]) ans = max(ans, sum);
    }

    cout << ans;

    return 0;
}


/** Simplicity is the final achievement **/

#include <bits/stdc++.h>

using namespace std;

const int N=2e6+73;
const int MD = 1e9 + 7;
long long INF = 1e17;

long long n,m,t,k,P;
long long arr[N];
long long x, y;
string s;

long long a[N], b[N];

int main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    /** ONLY FOOLS RUSH IN **/
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        long long sum = b[i];
        bool is = 0;
        for (int j = 0; j < n; j++) {
            if(i == j) continue;
            if(a[i] == a[j]) is = 1;
            if ((a[j] | a[i]) <= a[i]) {
                sum += b[j];
                // cerr << a[i] << " :" << a[j] << "\n";
            }
        }
        if (is) ans = max(ans, sum);
    }

    cout << ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
set<string> c;
const ll inf=(1e18);
const ll mod=1000000007;
const ll mod2=998244353;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
 map<ll,ll> mp;
int main(){
    int n;cin>>n;
    vector<ll> a(n),b(n),tmp;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
        if(mp[a[i]]==2)tmp.push_back(a[i]);
    }
    for(int i=0;i<n;i++){
        cin>>b[i];
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<tmp.size();j++){
            if((tmp[j]&a[i])==a[i]){
                       ans+=b[i];
                       break;
            }
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<int,int> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=0;
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    tmp|=a[i].fi;
    }
}
ll plus=0;
for(int i=n-1;i>=0;i--){
   if(a[i].fi<memo&&mp[a[i].fi]==1){
       bool check=true;
       for(int t=60;t>=0;t--){
           if((a[i].fi&(1LL<<t))==1&&(tmp&(1LL<<t))==0){
               check=false;
               break;
           }
       }
       if(check)plus=max(plus,a[i].se);
   }
}
cout<<ans+plus<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<ll,ll> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=-1;
vector<bool> used(n,true);
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    if(tmp==-1)tmp=a[i].fi;
    else tmp|=a[i].fi;
    used[i]=false;
    }
}
if(tmp==-1){
    cout<<0<<endl;
    return 0;
}
for(int i=n-1;i>=0;i--){
   if(used[i]&&a[i].fi<memo){
       if((tmp&a[i].fi)==a[i].fi){
           ans+=a[i].se;
       }
   }
}
cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<int,int> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=0;
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    tmp|=a[i].fi;
    }
}
for(int i=n-1;i>=0;i--){
   if(a[i].fi<memo&&mp[a[i].fi]==1){
       bool check=true;
       for(int t=60;t>=0;t--){
           if((a[i].fi&(1LL<<t))==1&&(tmp&(1LL<<t))==0){
               check=false;
               break;
               cout<<"a";
           }
       }
       if(check)ans+=a[i].se;
   }
}
cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<int,int> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=0;
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    tmp|=a[i].fi;
    }
}
for(int i=n-1;i>=0;i--){
   if(a[i].fi<memo&&mp[a[i].fi]==1){
       bool check=true;
       for(int t=60;t>=0;t--){
           if((a[i].fi&(1LL<<t))==1&&(tmp&(1LL<<t))==0){
               check=false;
               break;
           }
       }
       if(check)ans+=a[i].se;
   }
}
cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
set<string> c;
const ll inf=(1e18);
const ll mod=1000000007;
const ll mod2=998244353;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
 map<int,int> mp;
int main(){
    int n;cin>>n;
    vector<int> a(n),b(n),tmp;
    for(int i=0;i<n;i++){
        cin>>a[i];
        mp[a[i]]++;
        if(mp[a[i]]==2)tmp.push_back(a[i]);
    }
    for(int i=0;i<n;i++){
        cin>>b[i];
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<tmp.size();j++){
            if((tmp[j]&a[i])==a[i]){
                       ans+=b[i];
                       break;
            }
        }
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<ll,ll> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=0;
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    tmp|=a[i].fi;
    }
}
for(int i=n-1;i>=0;i--){
   if(a[i].fi<memo&&mp[a[i].fi]==1){
       bool check=true;
       for(int t=60;t>=0;t--){
           if((a[i].fi&(1LL<<t))==1&&(tmp&(1LL<<t))==0){
               check=false;
               break;
               cout<<"a";
           }
       }
       if(check)ans+=a[i].se;
   }
}
cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<ll,ll> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=0;
vector<bool> used(n,true);
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    tmp|=a[i].fi;
    used[i]=false;
    }
}
for(int i=n-1;i>=0;i--){
   if(used[i]){
       if((tmp&a[i].fi)==a[i].fi){
           ans+=a[i].se;
       }
   }
}
cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<ll,ll> P;
#define fi first
#define se second
#define all(v) (v).begin(),v.end()
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
map<ll,ll> mp;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<P> a(n);
for(int i=0;i<n;i++){cin>>a[i].fi;mp[a[i].fi]++;}
for(int i=0;i<n;i++)cin>>a[i].se;
sort(all(a));
ll ans=0;
ll memo=0,tmp=-1;
vector<bool> used(n,true);
for(int i=n-1;i>=0;i--){
    if(mp[a[i].fi]>1){ans+=a[i].se;
    memo=max(a[i].fi,memo);
    if(tmp==-1)tmp=a[i].fi;
    else tmp|=a[i].fi;
    used[i]=false;
    }
}
if(tmp==-1){
    cout<<0<<endl;
    return 0;
}
for(int i=n-1;i>=0;i--){
   if(used[i]){
       if((tmp&a[i].fi)==a[i].fi){
           ans+=a[i].se;
       }
   }
}
cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 1e4;
set <short> out[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n);
	vector <ll> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i].insert(j);
				}
			}
		}
	}
	int ver = n;
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		short for_del = -1;
		for (int i = 0; i < n; i++) {
			if (sz(out[i]) == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		out[for_del].clear();
		ver--;
		ans -= b[for_del];
		for (int i = 0; i < n; i++) {
			auto it = out[i].find(for_del);
			if (it != out[i].end()) {
				out[i].erase(it);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 7000;
bitset <7000> out[MAXN];

int cnt[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n);
	vector <ll> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	vector <int> actual;

	int ver = n;
	for (int i = 0; i < n; i++) {
		int better = 0;
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i][j] = true;
					better++;
				}
			}
		}
		cnt[i] = better;
		actual.eb(i);
	}
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		short for_del = -1;
		for (auto i : actual) {
			if (cnt[i] == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		ver--;
		actual.erase(find(all(actual), for_del));
		ans -= b[for_del];
		for (auto i : actual) {
			if (out[i][for_del]) {
				out[i][for_del] = false;
				cnt[i]--;
			}
			//~ out[i][for_del] = false;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 7000;
bitset <7000> out[MAXN];

int cnt[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n);
	vector <ll> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	vector <int> actual;
	for (int i = 0; i < n; i++) {
		int better = 0;
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i][j] = true;
					better++;
				}
			}
		}
		cnt[i] = better;
		if (better == n - 1) {
			ans -= b[i];
		} else {
			actual.eb(i);
		}
	}
	int ver = n;
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		short for_del = -1;
		for (auto i : actual) {
			if (cnt[i] == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		ver--;
		actual.erase(find(all(actual), for_del));
		ans -= b[for_del];
		for (auto i : actual) {
			if (out[i][for_del]) {
				out[i][for_del] = false;
				cnt[i]--;
			}
			//~ out[i][for_del] = false;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 7000;
bitset <7000> out[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n);
	vector <ll> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i][j] = true;
				}
			}
		}
	}
	int ver = n;
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		short for_del = -1;
		for (int i = 0; i < n; i++) {
			if ((int)out[i].count() == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		out[for_del].reset();
		ver--;
		ans -= b[for_del];
		for (int i = 0; i < n; i++) {
			out[i][for_del] = false;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 1e4;
set <int> out[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <int> a(n);
	vector <int> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i].insert(j);
				}
			}
		}
	}
	int ver = n;
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		int for_del = -1;
		for (int i = 0; i < n; i++) {
			if (sz(out[i]) == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		out[for_del].clear();
		ver--;
		ans -= b[for_del];
		for (int i = 0; i < n; i++) {
			auto it = out[i].find(for_del);
			if (it != out[i].end()) {
				out[i].erase(it);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 1e4;
set <int> out[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n);
	vector <ll> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i].insert(j);
				}
			}
		}
	}
	int ver = n;
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		int for_del = -1;
		for (int i = 0; i < n; i++) {
			if (sz(out[i]) == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		out[for_del].clear();
		ver--;
		ans -= b[for_del];
		for (int i = 0; i < n; i++) {
			auto it = out[i].find(for_del);
			if (it != out[i].end()) {
				out[i].erase(it);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define eb emplace_back
#define pb pop_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define sz(x) (int)x.size()
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define itn int
#define vpii vector<pii>
#define pdd pair<double,double>
#define vll vector<ll>
#define debug(x) cerr << #x << ": " << x << endl;

typedef long long ll;

using namespace std;

const int MAXN = 7000;
bitset <7000> out[MAXN];

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	vector <ll> a(n);
	vector <ll> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}
	ll ans = accumulate(all(b), 0ll);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i != j) {
				if (((a[i] ^ a[j]) & a[i]) != 0) {
					out[i][j] = true;
				}
			}
		}
	}
	int ver = n;
	vector <int> actual;
	for (int i = 0; i < n; i++) actual.eb(i);
	while (true) {
		if (ver == 1) {
			cout << 0;
			return 0;
		}
		short for_del = -1;
		for (auto i : actual) {
			if ((int)out[i].count() == ver - 1) {
				for_del = i;
				break;
			}
		}
		if (for_del == -1) {
			cout << ans;
			return 0;
		}
		out[for_del].reset();
		ver--;
		actual.erase(find(all(actual), for_del));
		ans -= b[for_del];
		for (auto i : actual) {
			out[i][for_del] = false;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, unordered_map<int, pair<int, unordered_set<int>>>& c, vector<vector<int>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    unordered_map<int, pair<int, unordered_set<int>>> big_map;
    vector<vector<int>> dv_vector(n);
    for(int i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, unordered_set<int>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t check_cnt = -1, ver_cnt = n;

    while (check_cnt != ver_cnt && ver_cnt>1) {
        check_cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == ver_cnt - 1) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    check_cnt = -1;
                    break;
                } else {
                    check_cnt++;
                }
            }
        }
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, map<int, pair<int, set<int>>>& c, vector<vector<int>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    map<int, pair<int, set<int>>> big_map;
    vector<vector<int>> dv_vector(n);
    for(int i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, set<int>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t check_cnt = -1, ver_cnt = n;

    while (check_cnt != ver_cnt && ver_cnt>1) {
        check_cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == ver_cnt - 1) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    check_cnt = -1;
                    break;
                } else {
                    check_cnt++;
                }
            }
        }
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, unordered_map<int64_t, pair<int64_t, unordered_set<int64_t>>>& c, vector<vector<int64_t>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    unordered_map<int64_t, pair<int64_t, unordered_set<int64_t>>> big_map;
    vector<vector<int64_t>> dv_vector(n);
    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, unordered_set<int64_t>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t check_cnt = -1, ver_cnt = n;

    while (check_cnt != ver_cnt && ver_cnt>1) {
        check_cnt = 0;
        for (int64_t i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == ver_cnt - 1) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    check_cnt = -1;
                    break;
                } else {
                    check_cnt++;
                }
            }
        }
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    int64_t sum_exp = 0, chel = 0;
    cin >> n;
    vector<int64_t> alg(n), exp(n), ans(60), two_and_more;
    vector<vector<int64_t>> dv_alg(n);
    map<int64_t,int64_t> map;
    bool status=true;
    for (int i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        alg[i] = x;
        ans[i] = 0;
        if (map[x]) {
            map[x]++;
        } else {
            map[x] = 1;
        }
    }

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        exp[i] = x;
    }

    for (int i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_alg[i].resize(60);
        int64_t dec = alg[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_alg[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    int64_t global_sum = 0;

    for (int i = 0; i < n; ++i) {
        if (map[alg[i]] > 1 ) {
            two_and_more.push_back(i);
            global_sum = global_sum + exp[i];
        }
    }

    if (two_and_more.size() == 0) {
        cout << 0;
    } else {
        for (int k = 0; k < n; ++k) {
            if (map[alg[k]] > 1) {
                continue;
            }
            for (int i = 0; i < two_and_more.size(); ++i) {
                int cnt_j = 0;
                for (int j = 0; j < 60; ++j) {
                    if (dv_alg[k][j] > dv_alg[two_and_more[i]][j]) {
                        break;
                    }
                    cnt_j = j;
                }
                if (cnt_j == 59) {
                    two_and_more.push_back(k);
                    global_sum = global_sum + exp[k];
                    break;
                }
            }
        }
        cout << global_sum;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    int64_t sum_exp = 0, chel = 0;
    cin >> n;
    vector<int> alg(n), exp(n), ans(60), two_and_more;
    vector<vector<int>> dv_alg(n);
    map<int,int> map;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        alg[i] = x;
        ans[i] = 0;
        if (map[x]) {
            map[x]++;
        } else {
            map[x] = 1;
        }
    }

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        exp[i] = x;
    }

    for (int i = 0; i < n; ++i) {
        int bin=0, j;
        dv_alg[i].resize(60);
        int dec = alg[i];
        for(j=0; dec>0; j++)
        {
            int z = dec % 2;
            dv_alg[i][j] = z;
            bin+=(dec % 2)*pow(10.0,j);
            dec/=2;
        }
    }

    int64_t global_sum = 0;

    for (int i = 0; i < n; ++i) {
        if (map[alg[i]] < 2 ) {
            continue;
        }
        int64_t sum = 0, chel = 0;
        for (int j = 0; j < n; ++j) {
            int cnt_k = 0;
            for (int k = 0; k < 60; ++k) {
                if (dv_alg[i][k] < dv_alg[j][k]) {
                    break;
                }
                cnt_k = k;
            }
            if (cnt_k == 59) {
                sum = sum + exp[j];
                chel++;
            }
        }
        if (chel > 1) {
            global_sum = max(global_sum, sum);
        }
    }

    cout << global_sum;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, map<int, pair<int, set<int>>>& c, vector<vector<int>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    map<int, pair<int, set<int>>> big_map;
    vector<vector<int>> dv_vector(n);
    for(int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, set<int>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t check_cnt = -1, ver_cnt = n;

    while (check_cnt != ver_cnt && ver_cnt>1) {
        check_cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == ver_cnt - 1) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    check_cnt = -1;
                    break;
                } else {
                    check_cnt++;
                }
            }
        }
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    int64_t sum_exp = 0, chel = 0;
    cin >> n;
    vector<int64_t> alg(n), exp(n), ans(60), two_and_more;
    vector<vector<int64_t>> dv_alg(n);
    map<int64_t,int64_t> map;
    bool status=true;
    for (int i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        alg[i] = x;
        if (map[x]) {
            map[x]++;
        } else {
            map[x] = 1;
        }
    }

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        exp[i] = x;
    }

    for (int i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_alg[i].resize(60);
        int64_t dec = alg[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_alg[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    int64_t global_sum = 0;

    for (int i = 0; i < n; ++i) {
        if (map[alg[i]] > 1 ) {
            two_and_more.push_back(i);
            global_sum = global_sum + exp[i];
        }
    }

    if (two_and_more.size() == 0) {
        cout << 0;
    } else {
        for (int k = 0; k < n; ++k) {
            if (map[alg[k]] > 1) {
                continue;
            }
            for (int i = 0; i < two_and_more.size(); ++i) {
                int cnt_j = 0;
                for (int j = 0; j < 60; ++j) {
                    if (dv_alg[k][j] > dv_alg[two_and_more[i]][j]) {
                        break;
                    }
                    cnt_j = j;
                }
                if (cnt_j == 59) {
                    two_and_more.push_back(k);
                    global_sum = global_sum + exp[k];
                    break;
                }
            }
        }
        cout << global_sum;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, map<int64_t, pair<int, set<int>>>& c, vector<vector<int64_t>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    map<int64_t, pair<int, set<int>>> big_map;
    vector<vector<int64_t>> dv_vector(n);
    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, set<int>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t max_ver_pow = n - 1, ver_cnt = n;

    while (ver_cnt - max_ver_pow == 1 && ver_cnt>0) {
        for (int i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == max_ver_pow) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    break;
                }
            } else {
                continue;
            }
            // cout << big_map[i].first << " ";
        }
        int cur_max_pow = 0;
        for (int64_t i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                cur_max_pow = max(cur_max_pow, big_map[i].first);
            }
        }
        max_ver_pow = cur_max_pow;
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, unordered_map<int, pair<int, unordered_set<int16_t>>>& c, vector<vector<int>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    unordered_map<int, pair<int, unordered_set<int16_t>>> big_map;
    vector<vector<int>> dv_vector(n);
    for(int i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, unordered_set<int16_t>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t check_cnt = -1, ver_cnt = n;

    while (check_cnt != ver_cnt && ver_cnt>1) {
        check_cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == ver_cnt - 1) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    check_cnt = -1;
                    break;
                } else {
                    check_cnt++;
                }
            }
        }
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    int64_t sum_exp = 0, chel = 0;
    cin >> n;
    vector<int> alg(n), exp(n), ans(60), two_and_more;
    vector<vector<int>> dv_alg(n);
    map<int,int> map;
    bool status=true;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        alg[i] = x;
        ans[i] = 0;
        if (map[x]) {
            map[x]++;
        } else {
            map[x] = 1;
        }
    }

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        exp[i] = x;
    }

    for (int i = 0; i < n; ++i) {
        int bin=0, j;
        dv_alg[i].resize(60);
        int dec = alg[i];
        for(j=0; dec>0; j++)
        {
            int z = dec % 2;
            dv_alg[i][j] = z;
            bin+=(dec % 2)*pow(10.0,j);
            dec/=2;
        }
    }

    int64_t global_sum = 0;

    for (int i = 0; i < n; ++i) {
        if (map[alg[i]] > 1 ) {
            two_and_more.push_back(i);
            global_sum = global_sum + exp[i];
        }
    }

    if (two_and_more.size() == 0) {
        cout << 0;
    } else {
        for (int k = 0; k < n; ++k) {
            if (map[alg[k]] > 1) {
                continue;
            }
            for (int i = 0; i < two_and_more.size(); ++i) {
                int cnt_j = 0;
                for (int j = 0; j < 60; ++j) {
                    if (dv_alg[k][j] > dv_alg[two_and_more[i]][j]) {
                        break;
                    }
                    cnt_j = j;
                }
                if (cnt_j == 59) {
                    two_and_more.push_back(k);
                    global_sum = global_sum + exp[k];
                    break;
                }
            }
        }
        cout << global_sum;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void matching (int64_t a, int64_t b, map<int64_t, pair<int64_t, set<int64_t>>>& c, vector<vector<int64_t>>& d) {
    for (int64_t i = 0; i < 60; ++i) {
        if (d[a][i] > d[b][i]) {
            c[a].first++;
            c[b].second.insert(a);
            break;
        }
    }
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int64_t n;
    cin >> n;
    vector<int64_t> input(n), exp(n);
    map<int64_t, pair<int64_t, set<int64_t>>> big_map;
    vector<vector<int64_t>> dv_vector(n);
    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        input[i] = x;
        big_map[i] = make_pair(0, set<int64_t>());
    }

    int64_t sum_exp = 0;

    for(int64_t i = 0; i < n; ++i) {
        int64_t x;
        cin >> x;
        exp[i] = x;
        sum_exp = sum_exp + x;
    }

    for (int64_t i = 0; i < n; ++i) {
        int64_t bin=0, j;
        dv_vector[i].resize(60);
        int64_t dec = input[i];
        for(j=0; dec>0; j++)
        {
            int64_t z = dec % 2;
            dv_vector[i][j] = z;
            bin+=(dec % 2)*pow((int64_t)10.0,j);
            dec/=2;
        }
    }

    // for (int i = 0; i < dv_vector[0].size(); ++i) {
    //     cout << dv_vector[0][i] << ' ';
    // }

    for (int64_t i = 0; i < n; ++i) {
        for (int64_t j = 0; j < n; ++j) {
            if (i == j) {
                continue;
            } else {
                matching(i, j, big_map, dv_vector);
            }
        }
    }

    int64_t max_ver_pow = n - 1, ver_cnt = n;

    while (ver_cnt - max_ver_pow == 1 && ver_cnt>0) {
        for (int64_t i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                if(big_map[i].first == max_ver_pow) {
                    for (auto& index:big_map[i].second) {
                        if (big_map.find(index) != big_map.end()) {
                            big_map[index].first--;
                        }
                    }
                    big_map.erase(i);
                    ver_cnt--;
                    sum_exp = sum_exp - exp[i];
                    break;
                }
            } else {
                continue;
            }
            // cout << big_map[i].first << " ";
        }
        int64_t cur_max_pow = 0;
        for (int64_t i = 0; i < n; ++i) {
            if (big_map.find(i) != big_map.end()) {
                cur_max_pow = max(cur_max_pow, big_map[i].first);
            }
        }
        max_ver_pow = cur_max_pow;
    }

    if (ver_cnt > 1) {
        cout << sum_exp;
    } else {
        cout << 0;
    }

    // cout << big_map[0] << ' ' << big_map[10];

    // cout << big_map[0].first << ' ';

    // for(auto& index:big_map[0].second) {
    //     cout << index << ' ';
    // }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    int d = n;
    for (int i = 1; i < n; i++)
    {
        if (s[i].first == s[i - 1].first)
        {
            d = i - 1;
            break;
        }
    }
    ll ans = 0;
    for (int i = d; i < n; i++)
    {
        if (good(s[d].first, s[i].first))
            ans += s[i].second;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    ll ans = 0;
    for (int i = 1; i < n; i++)
    {
        if (s[i].first == s[i - 1].first)
        {
            ll lans = 0;
            for (int j = i - 1; j < n; j++)
                if (good(s[i].first, s[j].first))
                    lans += s[j].second;
            ans = max(ans, lans);
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.begin(), s.end());
    ll ans = 0;
    for (int i = 0; i < n - 1; i++)
    {
        ll knw = 0;
        ll lans = s[i].second;
        int nb = 1;
        knw |= s[i].first;
        for (int j = i + 1; j < n; j++)
        {
            if (good(s[j].first, knw) and (s[j].first == s[j - 1].first or s[j].first == s[j + 1].first))
            {
                nb++;
                lans += s[j].second;
                knw |= s[j].first;
            }
        }
        if (nb >= 2)
            ans = max(lans, ans);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    ll ans = 0;
    for (int i = 1; i < n; i++)
    {
        if (s[i].first == s[i - 1].first)
        {
            ll lans = 0;
            for (int j = i - 1; j < n; j++)
                lans += s[j].second;
            ans = max(ans, lans);
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    vector<bool> used(n, false);
    int i = 0;
    while (i + 1 < n and s[i].first != s[i + 1].first)
        i++;
    ll knw = s[i].first;
    int nb = 1;
    ll ans = s[i].second;
    used[i] = true;
    while (i + 1 < n and s[i].first == s[i + 1].first)
    {
        ans += s[i + 1].second;
        nb++;
        used[i + 1] = true;
        knw |= s[i + 1].first;
        i++;
    }
    for (int i = 0; i < n; i++)
    {
        if (used[i])
            continue;
        if ((knw | s[i].first) == knw)
        {
            ans += s[i].second;
            nb++;
            used[i] = true;
        }
    }
    if (nb < 2)
        ans = 0;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    vector<bool> used(n, false);
    int nb = 0;
    ll knw = 0;
    ll ans = 0;
    for (int i = 1; i < n; i++)
    {
        if (s[i].first == s[i - 1].first)
        {
            knw |= s[i].first;
            if (!used[i])
            {
                nb++;
                ans += s[i].second;
                used[i] = true;
            }
            if (!used[i - 1])
            {
                nb++;
                ans += s[i - 1].second;
                used[i - 1] = true;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        if (used[i])
            continue;
        for (int j = 0; j < n; j++)
        {
            if (used[j] and (s[i].first | s[j].first) == s[j].first)
            {
                ans += s[i].second;
                nb++;
                used[i] = true;
                break;
            }
        }
    }
    if (nb < 2)
        ans = 0;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.begin(), s.end());
    ll ans = 0;
    for (int i = 0; i < n - 2; i++)
    {
        ll knw = 0;
        ll lans = s[i].second;
        int nb = 1;
        knw |= s[i].first;
        for (int j = i + 1; j < n; j++)
        {
            if (good(s[j].first, knw) and (good(s[j - 1].first, s[j].first) or good(s[j + 1].first, s[j].first)))
            {
                nb++;
                lans += s[j].second;
                knw |= s[j].first;
            }
        }
        if (nb >= 2)
            ans = max(lans, ans);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    int d = n;
    for (int i = 1; i < n; i++)
    {
        if (s[i].first == s[i - 1].first)
        {
            d = i - 1;
            break;
        }
    }
    ll ans = 0;
    for (int i = d; i < n; i++)
        ans += s[i].second;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.begin(), s.end());
    ll ans = 0;
    for (int i = 0; i < n - 1; i++)
    {
        ll knw = 0;
        ll lans = s[i].second;
        int nb = 1;
        knw |= s[i].first;
        for (int j = i + 1; j < n; j++)
        {
            if (good(s[j].first, knw) and (((good(s[j - 1].first, s[j].first)) or (j + 1 < n and good(s[j + 1].first, s[j].first))) or s[j].first == s[j - 1].first or (j + 1 < n and s[j].first == s[j + 1].first)))
            {
                nb++;
                lans += s[j].second;
                knw |= s[j].first;
            }
        }
        if (nb >= 2)
            ans = max(lans, ans);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.rbegin(), s.rend());
    int dd = n;
    for (int i = 1; i < n; i++)
    {
        if (s[i].first == s[i - 1].first)
        {
            dd = i - 1;
            break;
        }
    }
    ll ans = 0;
    for (int d = dd; d < n; d++)
    {
        ll lans = 0;
        for (int i = d; i < n; i++)
        {
            if (good(s[d].first, s[i].first))
                lans += s[i].second;
        }
        ans = max(lans, ans);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

bool good(ll a, ll b)
{
    while (a and b)
    {
        if (a % 2 == 0 and b % 2 == 1)
            return false;
        a /= 2;
        b /= 2;
    }
    return true;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<ll, ll>> s(n);
    for (int i = 0; i < n; i++)
        cin >> s[i].first;
    for (int i = 0; i < n; i++)
        cin >> s[i].second;
    sort(s.begin(), s.end());
    ll ans = 0;
    for (int i = 0; i < n - 2; i++)
    {
        ll knw = 0;
        ll lans = s[i].second;
        int nb = 1;
        knw |= s[i].first;
        for (int j = i + 1; j < n; j++)
        {
            if (good(s[j].first, knw) and (good(s[j].first, s[j - 1].first) or good(s[j].first, s[j + 1].first)))
            {
                nb++;
                lans += s[j].second;
                knw |= s[j].first;
            }
        }
        if (nb >= 2)
            ans = max(lans, ans);
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=7010;
const int oo=0x3f3f3f3f;
typedef unsigned long long ull;
ull read(){
	ull s=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())s=s*10+c-'0';
	return s;
}
struct qq{
	ull a;
	int b;
};
bool cmp(qq a,qq b){
	return a.a<b.a;
}
vector<qq>a;
vector<ull>b;
bool check(int t){
	for(int i=0;i<b.size();i++)
		if(a[t].a<=b[i]&&((a[t].a&b[i])==a[t].a))return true;
	//	else printf("%d %ulld %d\n",t,a[t].a,b[i]);
	return false;
}
int main(){
//	printf("%d %d",2<=3,(137506062337&206162624513));
	int n=read();
	for(int i=0;i<n;i++)
		a.push_back({read(),0});
	for(int i=0;i<n;i++)
		a[i].b=read();
	sort(a.begin(),a.end(),cmp);
	a.push_back({1ll*oo*oo,oo});
	n--;
	while(n&&a[n].a!=a[n-1].a)n--;
	if(n==0)printf("0");
	else{
		n++;
		ull ans=0;
		for(int i=0;i<n;i++)
			if(a[i].a==a[i+1].a){
				b.push_back(a[i].a);
				while(a[i].a==a[i+1].a)i++;
			}
		for(int i=0;i<n;i++)
			if(check(i))ans+=a[i].b;
		cout<<ans;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=7010;
const int oo=0x3f3f3f3f;
typedef unsigned long long ull;
ull read(){
	ull s=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())s=s*10+c-'0';
	return s;
}
struct qq{
	ull a;
	int b;
};
bool cmp(qq a,qq b){
	return a.a<b.a;
}
vector<qq>a;
vector<int>b;
bool check(int t){
	for(int i=0;i<b.size();i++)
		if(a[t].a<=b[i]&&((a[t].a&b[i])==a[t].a))return true;
	//	else printf("%d %d %d\n",t,a[t].a,b[i]);
	return false;
}
int main(){
//	printf("%d %d",2<=3,(2&3)==2);
	int n=read();
	for(int i=0;i<n;i++)
		a.push_back({read(),0});
	for(int i=0;i<n;i++)
		a[i].b=read();
	a.push_back({1ll*oo*oo,oo});
	sort(a.begin(),a.end(),cmp);
	n--;
	while(n&&a[n].a!=a[n-1].a)n--;
	if(n==0)printf("0");
	else{
		n++;
		int ans=0;
		for(int i=0;i<n;i++)
			if(a[i].a==a[i+1].a){
				b.push_back(a[i].a);
				while(a[i].a==a[i+1].a)i++;
			}
		for(int i=0;i<n;i++)
			if(check(i))ans+=a[i].b;
		printf("%d",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=7010;
const int oo=0x3f3f3f3f;
typedef unsigned long long ull;
ull read(){
	ull s=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())s=s*10+c-'0';
	return s;
}
struct qq{
	ull a;
	int b;
};
bool cmp(qq a,qq b){
	return a.a<b.a;
}
vector<qq>a;
vector<ull>b;
bool check(int t){
	for(int i=0;i<b.size();i++)
		if(a[t].a<=b[i]&&((a[t].a&b[i])==a[t].a))return true;
	//	else printf("%d %ulld %d\n",t,a[t].a,b[i]);
	return false;
}
int main(){
//	printf("%d %d",2<=3,(137506062337&206162624513));
	int n=read();
	for(int i=0;i<n;i++)
		a.push_back({read(),0});
	for(int i=0;i<n;i++)
		a[i].b=read();
	a.push_back({1ll*oo*oo,oo});
	sort(a.begin(),a.end(),cmp);
	n--;
	while(n&&a[n].a!=a[n-1].a)n--;
	if(n==0)printf("0");
	else{
		n++;
		ull ans=0;
		for(int i=0;i<n;i++)
			if(a[i].a==a[i+1].a){
				b.push_back(a[i].a);
				while(a[i].a==a[i+1].a)i++;
			}
		for(int i=0;i<n;i++)
			if(check(i))ans+=a[i].b;
		cout<<ans;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=7010;
const int oo=0x3f3f3f3f;
int read(){
	int f=1,s=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
	for(;c>='0'&&c<='9';c=getchar())s=s*10+c-'0';
	return f*s;
}
struct qq{
	int a,b;
};
bool cmp(qq a,qq b){
	return a.a<b.a;
}
vector<qq>a;
vector<int>b;
bool check(int t){
	for(int i=0;i<b.size();i++)
		if(a[t].a<=b[i]&&((a[t].a&b[i])==a[t].a))return true;
	//	else printf("%d %d %d\n",t,a[t].a,b[i]);
	return false;
}
int main(){
//	printf("%d %d",2<=3,(2&3)==2);
	int n=read();
	for(int i=0;i<n;i++)
		a.push_back({read(),0});
	for(int i=0;i<n;i++)
		a[i].b=read();
	a.push_back({oo,oo});
	sort(a.begin(),a.end(),cmp);
	n--;
	while(n&&a[n].a!=a[n-1].a)n--;
	if(n==0)printf("0");
	else{
		n++;
		int ans=0;
		for(int i=0;i<n;i++)
			if(a[i].a==a[i+1].a){
				b.push_back(a[i].a);
				while(a[i].a==a[i+1].a)i++;
			}
		for(int i=0;i<n;i++)
			if(check(i))ans+=a[i].b;
		printf("%d",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=7010;
const int oo=0x3f3f3f3f;
typedef unsigned long long ull;
ull read(){
	ull s=0;char c=getchar();
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())s=s*10+c-'0';
	return f*s;
}
struct qq{
	ull a;
	int b;
};
bool cmp(qq a,qq b){
	return a.a<b.a;
}
vector<qq>a;
vector<int>b;
bool check(int t){
	for(int i=0;i<b.size();i++)
		if(a[t].a<=b[i]&&((a[t].a&b[i])==a[t].a))return true;
	//	else printf("%d %d %d\n",t,a[t].a,b[i]);
	return false;
}
int main(){
//	printf("%d %d",2<=3,(2&3)==2);
	int n=read();
	for(int i=0;i<n;i++)
		a.push_back({read(),0});
	for(int i=0;i<n;i++)
		a[i].b=read();
	a.push_back({1ll*oo*oo,oo});
	sort(a.begin(),a.end(),cmp);
	n--;
	while(n&&a[n].a!=a[n-1].a)n--;
	if(n==0)printf("0");
	else{
		n++;
		int ans=0;
		for(int i=0;i<n;i++)
			if(a[i].a==a[i+1].a){
				b.push_back(a[i].a);
				while(a[i].a==a[i+1].a)i++;
			}
		for(int i=0;i<n;i++)
			if(check(i))ans+=a[i].b;
		printf("%d",ans);
	}
	return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<pair<long long int,long long int>>v(n);
for(int i=0;i<n;i++)
{
  v[i] = {arr[i],brr[i]};
}
sort(v.begin(),v.end());
long long temp = -1;
int i = 0;
for(i=0;i<n-1;i++)
{
  if(v[i].first==v[i+1].first)
     {
       temp = i;
     }
}
if(temp==-1)
  return 0;
long long int res = 0;
for(int i = 0;i<=temp+1;i++)
res+= v[i].second;
return res;

}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<pair<long long int,long long int>>v(n);
for(int i=0;i<n;i++)
{
  v[i] = {arr[i],brr[i]};
}
sort(v.begin(),v.end());

vector<long long int>temps;
int i = 0;
for(i=0;i<n;i++)
{
  while(i+1<n&&v[i].first==v[i+1].first)
     {
       i++;
     }
  if(i>0&&v[i].first==v[i-1].first)
     temps.push_back(i);
}
//cout<<temps.size();
if(temps.size()==0)
  return 0;
long long int res = 0, ans = INT_MIN;
for(int i=0;i<temps.size();i++)
{
  res = 0;
//  cout<<temps[i];
  for(int j=0;j<=temps[i];j++)
   {
     //cout<<v[i].second<<" ";
     if((v[temps[i]].first&v[j].first)!=0)
     res+= v[j].second;
   }
 ans = max(ans,res);
}
return ans;

}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<pair<long long int,long long int>>v(n);
for(int i=0;i<n;i++)
{
  v[i] = {arr[i],brr[i]};
}
sort(v.begin(),v.end());
long long temp = -1;
int i = 0;
for(i=0;i<n-1;i++)
{
  if(v[i].first==v[i+1].first)
     {
       temp = i;
     }
}
if(temp==-1)
  return 0;
long long int res = 0;
for(int i = 0;i<=temp+1;i++)
 { if((v[i].first&v[temp].first)!=0)
    res+= v[i].second;
  }
return res;

}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  unordered_map<long long, long long>count;
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
    count[arr[i]]++;
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
set<long long>st;;
for(auto it = count.begin();it!=count.end();it++)
{
  if(it->second>=2)
    st.insert(it->first);
}
long long ans= 0;
for(int i=0;i<n;i++)
{
  bool chk = false;
  for(auto it = st.begin();it!=st.end();it++)
  {
    if((arr[i]&*it)==arr[i])
        {
          chk = true;
          break;
        }
  }
  if(chk ==true)
    ans+= brr[i];

}
return ans;
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
long long  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  unordered_map<long long, long long>count;
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
    count[arr[i]]++;
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<long long>st;
for(auto it = count.begin();it!=count.end();it++)
{
  if(it->second>=2)
    st.push_back(it->first);
}
long long ans= 0;
for(int i=0;i<n;i++)
{
  bool chk = false;
  for(int j = 0;j<st.size();j++)
  {
    if((arr[i]&st[j])==arr[i])
        {
          chk = true;
          break;
        }
  }
  if(chk==true)
    {
      ans = ans+brr[i];
      //ans%= 1000000007;
    }

}
return ans;
}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<pair<long long int,long long int>>v(n);
for(int i=0;i<n;i++)
{
  v[i] = {arr[i],brr[i]};
}
sort(v.begin(),v.end());

vector<long long int>temps;
int i = 0;
for(i=0;i<n;i++)
{
  while(i+1<n&&v[i].first==v[i+1].first)
     {
       i++;
     }
  if(i>0&&v[i].first==v[i-1].first)
     temps.push_back(i);
}
//cout<<temps.size();
if(temps.size()==0)
  return 0;
long long int res = 0, ans = INT_MIN;
for(int i=0;i<temps.size();i++)
{
  res = 0;
//  cout<<temps[i];
  for(int j=0;j<=temps[i];j++)
   {
     //cout<<v[i].second<<" ";
     res+= v[j].second;
   }
 ans = max(ans,res);
}
return ans;

}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<pair<long long int,long long int>>v(n);
for(int i=0;i<n;i++)
{
  v[i] = {arr[i],brr[i]};
}
sort(v.begin(),v.end());
long long temp = -1;
int i = 0;
for(i=0;i<n-1;i++)
{
  if(v[i].first==v[i+1].first)
     {
       temp = i;
     }
}
if(temp==-1)
  return 0;
long long int res = 0;
for(int i = 0;i<=temp+1;i++)
 { if((v[i].first&v[temp].first)!=0)
    res+= v[i].second;
return res;

}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include "bits/stdc++.h"
using namespace std;
int  solve()
{
  int n;
  cin>>n;
  long long int arr[n],brr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  for(int i=0;i<n;i++)
  {
    cin>>brr[i];
  }
vector<pair<long long int,long long int>>v(n);
for(int i=0;i<n;i++)
{
  v[i] = {arr[i],brr[i]};
}
sort(v.begin(),v.end());

vector<long long int>temps;
int i = 0;
for(i=0;i<n;i++)
{
  while(i+1<n&&v[i].first==v[i+1].first)
     {
       i++;
     }
  if(i>0&&v[i].first==v[i-1].first)
     temps.push_back(i);
}
//cout<<temps.size();
if(temps.size()==0)
  return 0;
long long int res = 0, ans = INT_MIN;
for(int i=0;i<temps.size();i++)
{
  res = 0;
//  cout<<temps[i];
  for(int j=0;j<=temps[i];j++)
   {
     //cout<<v[i].second<<" ";
     if((v[temps[i]].first&v[j])!=0)
     res+= v[j].second;
   }
 ans = max(ans,res);
}
return ans;

}
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t =1;
//  cin>>t;
  while(t--)
   {
     cout<<solve();
     cout<<endl;
   }
  return 0;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
template <typename T> using ordered_set = tree<T, null_type, less<T>,
                            rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN //FOUT
#endif
	int n;
	cin >> n;
	ll a[n], b[n];
	vector<ll> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		v[i] = a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	sort(all(v));
	ll sum = 0;
	bool vis[n];
	memset(vis,0,sizeof vis);
	for (int i = 0; i < n; i++)
	{
		auto it1 = upper_bound(all(v), a[i]);
		auto it2 = lower_bound(all(v), a[i]);
		if (it1 - it2 <= 1&&!vis[i]) continue;
		for (int j =0; j < n; j++)
		{
			if (!vis[j]&&(a[i] | a[j]) == a[i])
			{
				sum += b[j];
				vis[i]=1;
				vis[j]=1;
			}
		}
	}
	cout << sum << endl;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
template <typename T> using ordered_set = tree<T, null_type, less<T>,
                            rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN //FOUT
#endif
	int n;
	cin >> n;
	ull a[n], b[n];
	vector<ull> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		v[i] = a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	sort(all(v));
	ull sum = 0;
	bool vis[n];
	bool ok[n];
	zero(ok);
	for (int i = 0; i < n; i++)
	{
		auto it1 = upper_bound(all(v), a[i]);
		auto it2 = lower_bound(all(v), a[i]);
		if (it1 - it2 > 1) ok[i] = 1;
	}
	memset(vis, 0, sizeof vis);
	for (int i = 0; i < n; i++)
	{
		if (ok[i]) {
			for (int j = 0; j < n; j++)
			{
				if (!vis[j] && (a[i] | a[j]) == a[i])
				{
					sum += b[j];
					vis[j] = 1;
				}
			}
		}
	}
	cout << sum << endl;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
template <typename T> using ordered_set = tree<T, null_type, less<T>,
                            rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN //FOUT
#endif
	int n;
	cin >> n;
	ull a[n], b[n];
	vector<ull> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		v[i] = a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	sort(all(v));
	ull sum = 0;
	bool vis[n];
	bool ok[n];
	zero(ok);
	for (int i = 0; i < n; i++)
	{
		auto it1 = upper_bound(all(v), a[i]);
		auto it2 = lower_bound(all(v), a[i]);
		if (it1 - it2 > 1) ok[i] = 1;
	}
	memset(vis, 0, sizeof vis);
	for (int i = 0; i < n; i++)
	{
		if (ok[i]) {
			for (int j = 0; j < n; j++)
			{
				if (!vis[j] && (a[i] | a[j]) == a[i])
				{
					sum += b[j];
					vis[i] = 1;
					vis[j] = 1;
				}
			}
		}
	}
	cout << sum << endl;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
template <typename T> using ordered_set = tree<T, null_type, less<T>,
                            rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN //FOUT
#endif
	int n;
	cin >> n;
	ull a[n], b[n];
	vector<ull> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		v[i] = a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	sort(all(v));
	ull sum = 0;
	bool vis[n];
	bool ok[n];
	zero(ok);
	for (int i = 0; i < n; i++)
	{
		auto it1 = upper_bound(all(v), a[i]);
		auto it2 = lower_bound(all(v), a[i]);
		if (it1 - it2 > 1) ok[i] = 1;
	}
	memset(vis, 0, sizeof vis);
	for (int i = 0; i < n; i++)
	{
		if (!vis[i] && ok[i]) {
			for (int j = 0; j < n; j++)
			{
				if (!vis[j] && (a[i] | a[j]) == a[i])
				{
					sum += b[j];
					vis[i] = 1;
					vis[j] = 1;
				}
			}
		}
	}
	cout << sum << endl;
}


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
template <typename T> using ordered_set = tree<T, null_type, less<T>,
                            rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN //FOUT
#endif
	int n;
	cin >> n;
	ll a[n], b[n];
	vector<ll> v(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		v[i] = a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin >> b[i];
	}
	sort(all(v));
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll sum = 0;
		auto it1 = upper_bound(all(v), a[i]);
		auto it2 = lower_bound(all(v), a[i]);
		if (it1 - it2 <= 1) continue;

		for (int j = 0; j < n; j++)
		{
			if ((a[i] | a[j]) == a[i])
			{
				sum += b[j];
			}
		}
		maxi = max(maxi, sum);
	}
	cout << maxi << endl;
}