#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
LL a[maxn],b[maxn],c[maxn];
bool vis[100];
vector<LL>v;
set<int>s;
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=N;i++) scanf("%lld",&b[i]);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            if(i==j) continue;
            else if(a[j]>a[i]) continue;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
//                cout<<"**"<<(1ll<<k)<<endl;
//                cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<endl;
//                cout<<(a[i]&(1ll<<k))<<" "<<(a[j]&(1ll<<k))<<endl;
                LL x=a[i]&(1ll<<k),y=a[j]&(1ll<<k);
                if(x==0&&y!=0)
                {
//                    cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<endl;
                    flag1=1;break;
                }
            }
            if(a[j]==a[i])//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
//                cout<<"i:"<<i<<" j:"<<j<<endl;
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) vis[v[i]]=true;
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=b[i];
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
bool vis[maxn];
vector<LL>v;
set<int>s;
struct Node
{
    LL v1,v2;
}a[maxn];
bool cmp(const Node a,const Node b)
{
    return a.v1>b.v1;
}
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v1);
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v2);
    sort(a+1,a+N+1,cmp);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=i+1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
                if(a[i].v1<(1ll<<k)) break;//比自己大就没意义了
                LL x=(a[i].v1&(1ll<<k)),y=(a[j].v1&(1ll<<k));
//                cout<<"x:"<<x<<" y:"<<y<<endl;
                if(x==0&&y!=0)
                {
                    flag1=1;break;
                }
            }
            if(j==i+1&&a[j].v1!=a[i].v1) break;//没有和本身相等的数
            if(a[j].v1==a[i].v1)//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) vis[v[i]]=true;
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=a[i].v2;
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
LL a[maxn],b[maxn],c[maxn];
bool vis[100];
vector<LL>v;
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=N;i++) scanf("%lld",&b[i]);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        LL sum=b[i];
        int flag=0;
        for(int j=1;j<=N;j++)//所有能加入进来的数
        {
            if(i==j) continue;
            else if(a[j]>a[i]) continue;
            sum+=b[j];
            if(a[j]==a[i])
            {
                flag=1;
            }
        }
        if(flag==1) ans=max(ans,sum);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
bool vis[maxn];
vector<LL>v;
set<int>s;
struct Node
{
    LL v1,v2;
}a[maxn];
bool cmp(const Node a,const Node b)
{
    return a.v1>b.v1;
}
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v1);
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v2);
    sort(a+1,a+N+1,cmp);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        for(int j=i+1;j<=N;j++)//所有能加入进来的数
        {
            if(j==i+1&&a[j].v1!=a[i].v1) break;//没有和本身相等的数
            else vis[i]=true;//有和本身相等的数
            if((a[i].v1|a[j].v1)==a[i].v1)
            {
                vis[j]=true;
            }
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=a[i].v2;
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
bool vis[maxn];
vector<LL>v;
set<int>s;
struct Node
{
    int v1,v2;
}a[maxn];
bool cmp(const Node a,const Node b)
{
    return a.v1>b.v2;
}
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v1);
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v2);
    sort(a+1,a+N+1,cmp);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=i+1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
                LL x=a[i].v1&(1ll<<k),y=a[j].v1&(1ll<<k);
                if(x==0&&y!=0)
                {
                    flag1=1;break;
                }
            }
            if(j==i+1&&a[j].v1!=a[i].v1) break;//没有和本身相等的数
            if(a[j].v1==a[i].v1)//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) vis[v[i]]=true;
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=a[i].v2;
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
bool vis[maxn];
vector<LL>v;
set<int>s;
struct Node
{
    LL v1,v2;
}a[maxn];
bool cmp(const Node a,const Node b)
{
    return a.v1>b.v1;
}
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v1);
    for(int i=1;i<=N;i++) scanf("%lld",&a[i].v2);
    sort(a+1,a+N+1,cmp);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=i+1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
                if(a[i]<(1ll<<k)) break;//比自己大就没意义了
                LL x=(a[i].v1&(1ll<<k)),y=(a[j].v1&(1ll<<k));
//                cout<<"x:"<<x<<" y:"<<y<<endl;
                if(x==0&&y!=0)
                {
                    flag1=1;break;
                }
            }
            if(j==i+1&&a[j].v1!=a[i].v1) break;//没有和本身相等的数
            if(a[j].v1==a[i].v1)//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) vis[v[i]]=true;
        }
    }
    for(int i=1;i<=N;i++)
    {
        if(vis[i]) ans+=a[i].v2;
    }
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=7e3+50;
LL a[maxn],b[maxn],c[maxn];
bool vis[100];
vector<LL>v;
set<int>s;
int main()
{
    LL N;scanf("%lld",&N);
    LL ans=0;
    for(int i=1;i<=N;i++) scanf("%lld",&a[i]);
    for(int i=1;i<=N;i++) scanf("%lld",&b[i]);
    for(int i=1;i<=N;i++)//以当前数为最大值
    {
        v.clear();
        v.push_back(i);
        int flag=0;
        for(int j=1;j<=N;j++)//所有能加入进来的数
        {
            int flag1=0;
            if(i==j) continue;
            else if(a[j]>a[i]) continue;
            for(LL k=0;k<=60;k++)//保证a[j]小于等于本身
            {
//                cout<<"**"<<(1ll<<k)<<endl;
//                cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<endl;
//                cout<<(a[i]&(1ll<<k))<<" "<<(a[j]&(1ll<<k))<<endl;
                LL x=a[i]&(1ll<<k),y=a[j]&(1ll<<k);
                if(x==0&&y!=0)
                {
//                    cout<<"i:"<<i<<" j:"<<j<<" k:"<<k<<endl;
                    flag1=1;break;
                }
            }
            if(a[j]==a[i])//必须要有一个等于本身的数
            {
                flag=1;
            }
            if(flag1==0)
            {
//                cout<<"i:"<<i<<" j:"<<j<<endl;
                v.push_back(j);
            }
        }
        if(flag==1)
        {
            for(int i=0;i<v.size();i++) s.insert(v[i]);
        }
    }
    set<int>::iterator it;
//    for(int i=0;;i++) cout<<s[i]<<endl;
    for(it=s.begin();it!=s.end();it++) ans+=b[*it];
    printf("%lld\n",ans);
    return 0;
}
/**
5
12 12 3 3 6
2 2  2 2 2
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll MOD= 1000000007;
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define pb push_back
#define mpr make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin>>n;
    ll a[n];
    map <ll,ll> mp;
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    ll b[n];
    for(ll i=0;i<n;i++)
        cin>>b[i];
    ll maxx=0;
    map <ll,ll> mp2;
    ll sum=0;
    for(auto x:mp)
    {
        if(x.second<=1)
            continue;
        for(ll j=0;j<n;j++)
        {
            ll num=a[j];
            ll flag=0;
            for(ll k=60;k>=0;k--)
            {

                if((num>>k)&1==1)
                {
                    //cout<<((a[i]>>k)&1)<<" "<<j<<" "<<k<<endl;
                    if(((x.first>>k)&1)==0)
                    {
                       // if(k==2)
                     //   cout<<"@@@"<<endl;
                        flag=1;
                        break;
                    }
                }

            }
            if(!flag)
            {
                mp2[j]=1;
            }
        }

    }
    for(ll j=0;j<n;j++)
    {
        if(mp2.count(j)>0)
            sum+=b[j];
    }
    cout<<sum<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll MOD= 1000000007;
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define pb push_back
#define mpr make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin>>n;
    ll a[n];
    map <ll,ll> mp;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    ll b[n];
    for(int i=0;i<n;i++)
        cin>>b[i];
    ll maxx=0;

    for(int i=0;i<n;i++)
    {
        if(mp[a[i]]<=1)
            continue;
        ll sum=b[i];
        for(int j=i+1;j<n;j++)
        {
            ll num=a[j];
            ll flag=0;
            for(int k=30;k>=0;k--)
            {

                if((num>>k)&1==1)
                {
                    //cout<<((a[i]>>k)&1)<<" "<<j<<" "<<k<<endl;
                    if(((a[i]>>k)&1)==0)
                    {
                       // if(k==2)
                     //   cout<<"@@@"<<endl;
                        flag=1;
                        break;
                    }
                }

            }
            if(!flag)
            {
                sum+=b[j];
            }
        }

        maxx=max(maxx,sum);
     //   cout<<a[i]<<" "<<maxx<<endl;
    }
    cout<<maxx<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll MOD= 1000000007;
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define pb push_back
#define mpr make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin>>n;
    ll a[n];
    map <ll,ll> mp;
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    ll b[n];
    for(ll i=0;i<n;i++)
        cin>>b[i];
    ll maxx=0;
    map <ll,ll> mp2;
    for(ll i=0;i<n;i++)
    {
        if(mp[a[i]]<=1)
            continue;
        if(mp2[a[i]])
            continue;
        mp2[a[i]]=1;
        ll sum=b[i];
        for(ll j=0;j<n;j++)
        {
            if(j==i)
                continue;
            ll num=a[j];
            ll flag=0;
            for(ll k=60;k>=0;k--)
            {

                if((num>>k)&1==1)
                {
                    //cout<<((a[i]>>k)&1)<<" "<<j<<" "<<k<<endl;
                    if(((a[i]>>k)&1)==0)
                    {
                       // if(k==2)
                     //   cout<<"@@@"<<endl;
                        flag=1;
                        break;
                    }
                }

            }
            if(!flag)
            {
                sum+=b[j];
            }
        }

        maxx=max(maxx,sum);
     //   cout<<a[i]<<" "<<maxx<<endl;
    }
    cout<<maxx<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll MOD= 1000000007;
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define pb push_back
#define mpr make_pair

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n;
    cin>>n;
    ll a[n];
    map <ll,ll> mp;
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    ll b[n];
    for(ll i=0;i<n;i++)
        cin>>b[i];
    ll maxx=0;
    map <ll,ll> mp2;
    for(ll i=0;i<n;i++)
    {
        if(mp[a[i]]<=1)
            continue;
        if(mp2[a[i]])
            continue;
        mp2[a[i]]=1;
        ll sum=b[i];
        for(ll j=i+1;j<n;j++)
        {
            ll num=a[j];
            ll flag=0;
            for(ll k=60;k>=0;k--)
            {

                if((num>>k)&1==1)
                {
                    //cout<<((a[i]>>k)&1)<<" "<<j<<" "<<k<<endl;
                    if(((a[i]>>k)&1)==0)
                    {
                       // if(k==2)
                     //   cout<<"@@@"<<endl;
                        flag=1;
                        break;
                    }
                }

            }
            if(!flag)
            {
                sum+=b[j];
            }
        }

        maxx=max(maxx,sum);
     //   cout<<a[i]<<" "<<maxx<<endl;
    }
    cout<<maxx<<endl;
}


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

//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ITER(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = (LL)INF * INF;

const int MAX = 7007;

int n;
pair<LL, int> a[MAX];

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	FOR(i, 0, n)
		cin >> a[i].first;
	LL ans = 0;
	FOR(i, 0, n)
	{
		cin >> a[i].second;
		ans += a[i].second;
	}
	sort(a, a + n);
	FOR(i, 0, n)
	{
		bool ok = false;
		FOR(j, 0, i)
			ok |= (a[i].first & (((1LL << 60) - 1) ^ a[j].first)) == 0;
		if(ok)
			break;
		ans -= a[i].second;
	}
	cout << ans << "\n";
	return 0;
}


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

//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ITER(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = (LL)INF * INF;

const int MAX = 7007;

int n;
LL a[MAX];
int b[MAX];

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	FOR(i, 0, n)
		cin >> a[i];
	FOR(i, 0, n)
		cin >> b[i];
	LL ans = 0;
	FOR(i, 0, n)
	{
		int cnt = 0;
		LL cur = 0;
		FOR(j, 0, n)
		{
			cnt += a[i] == a[j];
			if((a[j] & a[i]) == a[j])
				cur += b[j];
		}
		if(cnt > 1)
			ans = max(ans, cur);
	}
	cout << ans << "\n";
	return 0;
}


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

//#pragma GCC optimize("O3")
//#pragma GCC target("sse4")

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ITER(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = (LL)INF * INF;

const int MAX = 7007;

int n;
LL a[MAX];
int b[MAX];
bool is[MAX];

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	FOR(i, 0, n)
		cin >> a[i];
	FOR(i, 0, n)
		cin >> b[i];
	LL ans = 0;
	FOR(i, 0, n)
	{
		int cnt = 0;
		FOR(j, 0, n)
			cnt += a[i] == a[j];
		if(cnt > 1)
			FOR(j, 0, n)
				if((a[j] & a[i]) == a[j])
					is[j] = true;
	}
	FOR(i, 0, n)
		if(is[i])
			ans += b[i];
	cout << ans << "\n";
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;

ll n,anw,a[7004],b[7005],ck[7005];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i!=j&&a[i]==a[j])
				ck[i]=ck[j]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i!=j)
			{
				if(((a[i]^a[j])|a[i])==a[i]&&ck[i]==1)
					ck[j]=1;
			}
	for(int i=1;i<=n;i++)
		if(ck[i]==1)
			anw+=b[i];
	cout<<anw;
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;

ll n,anw,a[7005];
int b[7005],ck[7006];
vector<int> g[7006];
map<ll,ll> mp;

void dfs(ll a)
{
	ck[a]=1;anw+=(ll)b[a];
	for(int i=0;i<g[a].size();i++)
		if(!ck[g[a][i]])
			dfs(g[a][i]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i],mp[a[i]]++;
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i!=j)
			{
				if(((a[i]^a[j])|a[i])==a[i])
					g[i].push_back(j);
			}
	for(int i=1;i<=n;i++)
	{
		if(mp[a[i]]>=2&&!ck[i])
			dfs(i);
	}
	cout<<anw;
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;

ll n,anw,a[7005],b[7005],ck[7005];
vector<ll> g[7006];
map<ll,ll> mp;

void dfs(ll a)
{
	ck[a]=1;anw+=b[a];
	for(int i=0;i<g[a].size();i++)
		if(!ck[g[a][i]])
			dfs(g[a][i]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i],mp[a[i]]++;
	for(int i=1;i<=n;i++)
		cin>>b[i];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			if(i!=j)
			{
				if(((a[i]^a[j])|a[i])==a[i])
					g[i].push_back(j);
			}
	for(int i=1;i<=n;i++)
	{
		if(mp[a[i]]>=2&&!ck[i])
			dfs(i);
	}
	cout<<anw;
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;

ll anw,n,a[7005],b[7004],ck[7004];
vector<ll> g[7006];
vector<pi> st;
bitset<64> bt[7005];

void dfs(ll a)
{
	anw+=b[a],ck[a]=1;
	for(int i=0;i<g[a].size();i++)
		if(!ck[g[a][i]])
			dfs(g[a][i]);
}

int main()
{
	//ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i],st.push_back({a[i],i});
	for(int i=1;i<=n;i++)
		cin>>b[i];
	sort(st.begin(),st.end());
	for(int i=1;i<=n;i++)
		for(int j=60;j>=0;j--)
			if(a[i]&(1<<j))
				bt[i][j]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(((bt[i]^bt[j])|bt[i])==bt[i])
				g[i].push_back(j);
		}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(a[i]==a[j]&&!ck[i]) dfs(i);
		}
	cout<<anw;
	return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0;

map<ll, ll> counter;
pair<ll,ll> p[LIMIT];
vector<ll> donors;

int main(){
    scanf("%lld", &n);

    for(int i = 0; i < n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 0; i < n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(int i = 0; i < n; i++) {
        if(counter[p[i].first] > 1) {
            donors.push_back(p[i].first);
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < donors.size(); j++) {
            if((p[i].first & donors[j]) == p[i].first) {
                r += p[i].second;
                break;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0, strong = 0;

ll  skillSets[61];
map<ll, ll> counter;
pair<ll,ll> p[LIMIT];

int main(){
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] > 1) {
            for(ll j = 0; j <= 60; j++) {
                if(p[i].first & (1 << j)) {
                    skillSets[j] = max(skillSets[j] , p[i].first);
                }
            }

            strong++;
            r += p[i].second;
        }
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] == 1 && strong > 0) {
            bool add = true;
            ll maxSkillSet = -1;

            for(ll j = 0; j <= 60; j++) {
                if(p[i].first & (1 << j)) {

                    add = add && skillSets[j] > 0;

                    if(maxSkillSet == -1) {
                        maxSkillSet = skillSets[j];
                    } else if(maxSkillSet != skillSets[j]) {
                        add = false;
                    }

                }
            }

            if(add) {
                r += p[i].second;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0, strong = 0;

ll  skillSets[61];
map<ll, ll> counter;
pair<ll,ll> p[LIMIT];

int main(){
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] > 1) {
            for(ll j = 0; j <= 60; j++) {
                if(p[i].first & ((ll)1 << j)) {
                    skillSets[j] = max(skillSets[j] , p[i].first);
                }
            }

            strong++;
            r += p[i].second;
        }
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] == 1 && strong > 0) {
            bool add = true;
            ll maxSkillSet = -1;

            for(ll j = 0; j <= 60; j++) {
                if(p[i].first & ((ll)1 << j)) {

                    add = add && skillSets[j] > 0;

                    if(maxSkillSet == -1) {
                        maxSkillSet = skillSets[j];
                    } else if(maxSkillSet != skillSets[j]) {
                        add = false;
                    }

                }
            }

            if(add) {
                r += p[i].second;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0;

ll  skillSets[62];
map<ll, ll> counter;
pair<ll,ll> p[LIMIT];

int main(){
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] > 1) {
            for(ll j = 0; j <= 61; j++) {
                if(p[i].first & ((ll)1 << j)) {
                    skillSets[j] = max(skillSets[j] , p[i].first);
                }
            }
            r += p[i].second;
        }
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] == 1 && r > 0) {
            bool add = true;
            ll maxSkillSet = -1;

            for(ll j = 0; j <= 61; j++) {
                if(p[i].first & ((ll)1 << j)) {
                    add = add && skillSets[j] > 0;

                    if(maxSkillSet == -1) {
                        maxSkillSet = skillSets[j];
                    } else if(maxSkillSet != skillSets[j]) {
                        add = false;
                    }

                }
            }

            if(add) {
                r += p[i].second;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0;

map<ll, ll> counter;
pair<ll,ll> p[LIMIT];
vector<int> donors;

int main(){
    scanf("%lld", &n);

    for(int i = 0; i < n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 0; i < n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(int i = 0; i < n; i++) {
        if(counter[p[i].first] > 1) {
            donors.push_back(p[i].first);
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < donors.size(); j++) {
            if((p[i].first & donors[j]) == p[i].first) {
                r += p[i].second;
                break;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0, strong = 0;

ll  skillSets[61];
map<ll, ll> counter;
pair<ll,ll> p[LIMIT];

int main(){
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(int i = 1; i <= n; i++) {
        if(counter[p[i].first] > 1) {
            for(int j = 0; j <= 60; j++) {
                if(p[i].first & (1 << j)) {
                    skillSets[j] = max(skillSets[j] , p[i].first);
                }
            }

            strong++;
            r += p[i].second;
        }
    }

    for(int i = 1; i <= n; i++) {
        if(counter[p[i].first] == 1 && strong > 0) {
            bool add = true;
            ll maxSkillSet = -1;

            for(int j = 0; j <= 60; j++) {
                if(p[i].first & (1 << j)) {

                    add = add && skillSets[j] > 0;

                    if(maxSkillSet == -1) {
                        maxSkillSet = skillSets[j];
                    } else if(maxSkillSet != skillSets[j]) {
                        add = false;
                    }

                }
            }

            if(add) {
                r += p[i].second;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0, strong = 0;

bool  skillSets[61];
map<ll, int> counter;
pair<ll,ll> p[LIMIT];

int main(){
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(int i = 1; i <= n; i++) {
        if(counter[p[i].first] > 1) {
            for(int j = 0; j <= 60; j++) {
                if(p[i].first & (1 << j)) {
                    skillSets[j] = true;
                }
            }

            strong++;
            r += p[i].second;
        }
    }

    for(int i = 1; i <= n; i++) {
        if(counter[p[i].first] == 1 && strong > 0) {
            bool add = true;

            for(int j = 0; j <= 60; j++) {
                if(p[i].first & (1 << j)) {

                    add = add && skillSets[j];

                }
            }

            if(add) {
                r += p[i].second;
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<algorithm>
#include<map>
#include<fstream>
#include<cmath>
#include<queue>
#include<string.h>
#include<stack>
#include<deque>
#include<list>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
const int LIMIT = 7e3 + 7;
const int MOD = 1e9 + 7;

ll n, r = 0;

map<ll, ll> counter;
pair<ll,ll> p[LIMIT];

int main(){
    scanf("%lld", &n);

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].first);
        counter[p[i].first]++;
    }

    for(int i = 1; i <= n; i++) {
        scanf("%lld", &p[i].second);
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] > 1) {
            r += p[i].second;
        }
    }

    for(ll i = 1; i <= n; i++) {
        if(counter[p[i].first] == 1 && r > 0) {
            for(ll j = 1; j <= n; j++) {
                if(counter[p[j].first] > 1) {
                    if((p[i].first & p[j].first) == p[i].first) {
                        r += p[i].second;
                        break;
                    }
                }
            }
        }
    }

    printf("%lld", r);
    return EXIT_SUCCESS;
}


#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <bitset>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef pair<pli, int> tlii;
typedef pair<pair<long long, long long>, long long> tlll;

const int N = 7010;
const int K = 24;
const int M = 10000005;
const int mod = int(1e9) + 7;
const long long inf = (long long)(1e18);
const long double eps = 1e-9;

pair<long long, long long> a[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].first);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].second);
  }
  sort(a, a + n);
  long long ans = 0;
  long long t = 0;
  for (int i = 1; i < n; ++i) {
    if (a[i].first == a[i - 1].first) {
      t |= a[i].first;
    }
  }
  for (int i = 0; i < n; ++i) {
    if ((a[i].first | t) == t) {
      ans += a[i].second;
    }
  }
  printf("%lld\n", ans);
}

int main() {
#ifdef YSU1
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <bitset>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef pair<pli, int> tlii;
typedef pair<pair<long long, long long>, long long> tlll;

const int N = 7010;
const int K = 24;
const int M = 10000005;
const int mod = int(1e9) + 7;
const long long inf = (long long)(1e18);
const long double eps = 1e-9;

pair<long long, long long> a[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].first);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].second);
  }
  sort(a, a + n);
  long long ans = 0;
  long long t = 0;
  bool b = false;
  for (int i = 1; i < n; ++i) {
    if (a[i].first == a[i - 1].first) {
      t |= a[i].first;
      b = true;
    }
  }
  if (!b) {
    puts("0");
    return;
  }
  for (int i = 0; i < n; ++i) {
    if ((a[i].first | t) == t) {
      ans += a[i].second;
    }
  }
  printf("%lld\n", ans);
}

int main() {
#ifdef YSU1
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <bitset>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef pair<pli, int> tlii;
typedef pair<pair<long long, long long>, long long> tlll;

const int N = 7010;
const int K = 24;
const int M = 10000005;
const int mod = int(1e9) + 7;
const long long inf = (long long)(1e18);
const long double eps = 1e-9;

pair<long long, long long> a[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].first);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].second);
  }
  sort(a, a + n);
  long long sum = a[0].second;
  long long ans = 0;
  for (int i = 1; i < n; ++i) {
    sum += a[i].second;
    if (a[i].first == a[i - 1].first) {
      ans = max(ans, sum);
    }
  }
  printf("%lld\n", ans);

}

int main() {
#ifdef YSU1
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <bitset>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef pair<pli, int> tlii;
typedef pair<pair<long long, long long>, long long> tlll;

const int N = 7010;
const int K = 24;
const int M = 10000005;
const int mod = int(1e9) + 7;
const long long inf = (long long)(1e18);
const long double eps = 1e-9;

pair<long long, long long> a[N];
bool mark[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].first);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].second);
  }
  sort(a, a + n);
  long long ans = 0;
  bool b = false;
  for (int i = 1; i < n; ++i) {
    if (a[i].first == a[i - 1].first) {
      b = true;
    }
  }
  if (!b) {
    puts("0");
    return;
  }
  for (int i = 1; i < n; ++i) {
    if (a[i].first == a[i - 1].first) {
      for (int j = 0; j <= i; ++j) {
        if (mark[j]) {
          continue;
        }
        if ((a[i].first | a[j].first) == a[i].first) {
          mark[j] = true;
          ans += a[j].second;
        }
      }
    }
  }
  printf("%lld\n", ans);
}

int main() {
#ifdef YSU1
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include <unordered_map>
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <bitset>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef pair<pli, int> tlii;
typedef pair<pair<long long, long long>, long long> tlll;

const int N = 7010;
const int K = 24;
const int M = 10000005;
const int mod = int(1e9) + 7;
const long long inf = (long long)(1e18);
const long double eps = 1e-9;

pair<long long, long long> a[N];

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].first);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &a[i].second);
  }
  sort(a, a + n);
  long long sum = a[0].second;
  long long ans = 0;
  for (int i = 1; i < n; ++i) {
    sum += a[i].second;
    if (a[i].first == a[i - 1].first) {
      bool poss = true;
      for (int j = 0; j < i; ++j) {
        if ((a[j].first | a[i].first) > a[i].first) {
          poss = false;
          break;
        }
      }
      if (poss) {
        ans = max(ans, sum);
      }
    }
  }
  printf("%lld\n", ans);

}

int main() {
#ifdef YSU1
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
  return 0;
}


#include<iostream>
#include<vector>

using namespace std;

long long A[7005];
int B[7005],Viz[7005],Covered[7005],VizPerTot[7005],CrPath[7005];
vector<int> V[7005];

long long dfs(int nod)
{
    Viz[nod]=1;
    CrPath[nod]=1;

    for(auto fiu:V[nod])
    {
        if(Viz[fiu] && Covered[fiu])
            Covered[nod]=1;

        if(Viz[fiu] && CrPath[fiu])
            Covered[nod]=1;
    }

    long long sum=B[nod];
    for(auto fiu:V[nod])
    {
        if(Viz[fiu])
            continue;
        sum+=dfs(fiu);
        if(Covered[fiu])
            Covered[nod]=1;
    }

    CrPath[nod]=0;
    if(!Covered[nod])
        return 0;
    return sum;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;
    for(int i=1; i<=n; i++)
        cin>>A[i];

    for(int i=1; i<=n; i++)
        cin>>B[i];

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(((A[i]^A[j])&A[i])==0 && i!=j)
                V[i].push_back(j);

    long long rez=0LL;
    for(int i=1; i<=n; i++)
    {
        if(VizPerTot[i])
            continue;
        for(int j=1; j<=n; j++)
        {
            Viz[j]=0;
            Covered[j]=0;
        }

        long long sum=dfs(i);

        if(sum!=B[i])
        {
            for(int j=1; j<=n; j++)
                if(Viz[j] && Covered[j] && !VizPerTot[j])
                {
                    rez+=B[j];
                    VizPerTot[j]=1;
                }
        }
    }
    cout<<rez<<"\n";
    return 0;
}


#include<iostream>
#include<vector>

using namespace std;

long long A[7005];
int B[7005],Viz[7005],Covered[7005],VizPerTot[7005],CrPath[7005];
vector<int> V[7005];

long long dfs(int nod)
{
    Viz[nod]=1;
    CrPath[nod]=1;

    for(auto fiu:V[nod])
    {
        if(Viz[fiu] && Covered[fiu])
            Covered[nod]=1;

        if(Viz[fiu] && CrPath[fiu])
            Covered[nod]=1;
    }

    long long sum=B[nod];
    for(auto fiu:V[nod])
    {
        if(Viz[fiu])
            continue;
        sum+=dfs(fiu);
        if(Covered[fiu])
            Covered[nod]=1;
    }

    CrPath[nod]=0;
    if(!Covered[nod])
        return 0;
    return sum;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;
    for(int i=1; i<=n; i++)
        cin>>A[i];

    for(int i=1; i<=n; i++)
        cin>>B[i];

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(((A[i]^A[j])&A[i])==0 && i!=j)
                V[i].push_back(j);

    long long rez=0LL;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            Viz[j]=0;
            Covered[j]=0;
        }

        long long sum=dfs(i);

        if(sum!=B[i])
        {
            for(int j=1; j<=n; j++)
                if(Viz[j] && Covered[j] && !VizPerTot[j])
                {
                    rez+=B[j];
                    VizPerTot[j]=1;
                }
        }
    }
    cout<<rez<<"\n";
    return 0;
}


#include<iostream>
#include<vector>

using namespace std;

long long A[7005];
int B[7005],Viz[7005],Covered[7005],VizPerTot[7005];
vector<int> V[7005];

long long dfs(int nod)
{
    Viz[nod]=1;

    for(auto fiu:V[nod])
        if(Viz[fiu])
            Covered[nod]=1;

    long long sum=B[nod];
    for(auto fiu:V[nod])
    {
        if(Viz[fiu])
            continue;
        sum+=dfs(fiu);
        if(Covered[fiu])
            Covered[nod]=1;
    }

    if(!Covered[nod])
        return 0;
    return sum;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;
    for(int i=1; i<=n; i++)
        cin>>A[i];

    for(int i=1; i<=n; i++)
        cin>>B[i];

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(((A[i]^A[j])&A[i])==0 && i!=j)
                V[i].push_back(j);

    long long rez=0LL;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            Viz[j]=0;
            Covered[j]=0;
        }

        long long sum=dfs(i);

        if(sum!=B[i])
        {
            for(int j=1; j<=n; j++)
                if(Viz[j] && Covered[j] && !VizPerTot[j])
                {
                    rez+=B[j];
                    VizPerTot[j]=1;
                }
        }
    }
    cout<<rez<<"\n";
    return 0;
}


#include<iostream>
#include<vector>

using namespace std;

long long A[7005];
int B[7005],Viz[7005],Covered[7005];
vector<int> V[7005];

long long dfs(int nod)
{
    Viz[nod]=1;

    for(auto fiu:V[nod])
        if(Viz[fiu])
            Covered[nod]=1;

    long long sum=B[nod];
    for(auto fiu:V[nod])
    {
        if(Viz[fiu])
            continue;
        sum+=dfs(fiu);
        if(Covered[fiu])
            Covered[nod]=1;
    }

    if(!Covered[nod])
        return 0;
    return sum;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;
    for(int i=1; i<=n; i++)
        cin>>A[i];

    for(int i=1; i<=n; i++)
        cin>>B[i];

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(((A[i]^A[j])&A[i])==0 && i!=j)
                V[i].push_back(j);

    long long rez=0LL;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            Viz[j]=0;
            Covered[j]=0;
        }

        long long sum=dfs(i);

        if(sum!=B[i])
            rez=max(rez,sum);
    }
    cout<<rez<<"\n";
    return 0;
}


#include<iostream>
#include<vector>

using namespace std;

long long A[7005];
int B[7005],Viz[7005],Covered[7005],VizPerTot[7005],CrPath[7005];
vector<int> V[7005];

void dfs(int nod)
{
    Viz[nod]=1;
    CrPath[nod]=1;

    for(auto fiu:V[nod])
    {
        if(Covered[fiu])
            Covered[nod]=1;

        if(Viz[fiu] && CrPath[fiu])
            Covered[nod]=1;
    }

    for(auto fiu:V[nod])
    {
        if(Viz[fiu] || Covered[fiu])
            continue;
        dfs(fiu);
        if(Covered[fiu])
            Covered[nod]=1;
    }

    CrPath[nod]=0;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin>>n;
    for(int i=1; i<=n; i++)
        cin>>A[i];

    for(int i=1; i<=n; i++)
        cin>>B[i];

    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(((A[i]^A[j])&A[i])==0 && i!=j)
                V[i].push_back(j);

    long long rez=0LL;
    for(int i=1; i<=n; i++)
    {
        if(VizPerTot[i])
            continue;
        for(int j=1; j<=n; j++)
            Viz[j]=0;

        dfs(i);

        for(int j=1; j<=n; j++)
            if(Viz[j] && Covered[j] && !VizPerTot[j])
            {
                rez+=B[j];
                VizPerTot[j]=1;
            }
    }
    cout<<rez<<"\n";
    return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<unordered_map>
#include<algorithm>
#include<bitset>
#include<queue>
#include <math.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define PIII pair<pair<int,int>,int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define M (L+R)/2
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define int long long
const int N = 1000*1000 + 7;

int test;

int n;

int a[N],b[N];

map<int,int>m,ok;

int res;

VI to_check;

void solve()
{
	cin >> n;

	FOR(i,1,n){
		cin >> a[i];

		m[a[i]]++;
	}

	FOR(i,1,n){
		cin >> b[i];
	}

	for(auto it:m){
		if (it.nd >= 2){
			ok[it.st] = 1;
		}else{
			to_check.pb(it.st);
		}
	}

	for(auto it:ok){
		for(auto it1:to_check){
			if ((it1 & it.st) == it1){
				ok[it1] = 1;
			}
		}
	}

	FOR(i,1,n){
		if (ok[a[i]]){
			// debug(i,a[i]);
			res += b[i];
		}
	}

	cout << res;

    return;
}

int32_t main()
{
    boost

    test=1;

	for(int i=1; i <= test; i++){
        solve();
	}

    return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<unordered_map>
#include<algorithm>
#include<bitset>
#include<queue>
#include <math.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define PIII pair<pair<int,int>,int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define M (L+R)/2
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define int long long
const int N = 1000*1000 + 7;

int test;

int n;

int a[N],b[N];

unordered_map<int,int>m,ok;

int res;

VI to_check;

void solve()
{
	cin >> n;

	FOR(i,1,n){
		cin >> a[i];

		m[a[i]]++;
	}

	FOR(i,1,n){
		cin >> b[i];
	}

	for(auto it:m){
		if (it.nd >= 2){
			ok[it.st] = 1;
		}else{
			to_check.pb(it.st);
		}
	}

	for(auto it:ok){
		for(auto it1:to_check){
			if ((it1 & it.st) == it1){
				ok[it1] = 1;
			}
		}
	}

	FOR(i,1,n){
		if (ok[a[i]]){
			res += b[i];
		}
	}

	cout << res;

    return;
}

int32_t main()
{
    boost

    test=1;

	for(int i=1; i <= test; i++){
        solve();
	}

    return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<unordered_map>
#include<algorithm>
#include<bitset>
#include<queue>
#include <math.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define PIII pair<pair<int,int>,int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define M (L+R)/2
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define int long long
const int N = 1000*1000 + 7;

int test;

int n;

int a[N],b[N];

unordered_map<int,int>m,ok;

int res;

VI to_check;

void solve()
{
	cin >> n;

	FOR(i,1,n){
		cin >> a[i];

		m[a[i]]++;
	}

	FOR(i,1,n){
		cin >> b[i];
	}

	for(auto it:m){
		if (it.nd >= 2){
			ok[it.st] = 1;
		}else{
			to_check.pb(it.st);
		}
	}

	for(auto it:ok){
		for(auto it1:to_check){
			if ((it1 & it.st) == it1){
				ok[it1] = 1;
			}
		}
	}

	FOR(i,1,n){
		if (ok[a[i]]){
			debug(i,a[i]);
			res += b[i];
		}
	}

	cout << res;

    return;
}

int32_t main()
{
    boost

    test=1;

	for(int i=1; i <= test; i++){
        solve();
	}

    return 0;
}


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

#define ll long long
#define sz(a) ((int)a.size())
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define dbg(a) cout<<#a<<" = "<<a<<endl
#define dbg2(a,b) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<endl
#define dbg3(a,b,c) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<endl
#define dbg4(a,b,c,d) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<", "<<#d<<" = "<<d<<endl
#define ld long double
#define pii pair<int,int>
#define vi vector<int>

const int mod = 1e9 + 7;

int pos[7001];
ll v[7001], b[7001];

bool check(ll x,ll y){
    return ((x|y) == x);
}

void solve()
{
    int n;cin>>n;
    for(int i=0;i<n;i++) cin>>v[i];
    for(int i=0;i<n;i++) cin>>b[i];
    ll all = 0, pos = 0;
    vector<ll> vec;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(v[i]==v[j]){
                vec.pb(v[i]);
                break;
            }
        }
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        for(ll val:vec){
            if(check(val, v[i])){
                ans+=b[i];
                break;
            }
        }
    }
    cout<<ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << fixed << setprecision(11);
    clock_t beg=clock();
    int t=1, n;
    // cin>>t;
    // n = t;
    while(t--){
        // cout << "Case #" << n-t << ": ";
        solve();
    }
    //
    clock_t end=clock();
    fprintf(stderr, "%lf sec\n", (double)(end-beg)/(CLOCKS_PER_SEC));
    return 0;
}


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

#define ll long long
#define sz(a) ((int)a.size())
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define dbg(a) cout<<#a<<" = "<<a<<endl
#define dbg2(a,b) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<endl
#define dbg3(a,b,c) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<endl
#define dbg4(a,b,c,d) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<", "<<#d<<" = "<<d<<endl
#define ld long double
#define pii pair<int,int>
#define vi vector<int>

const int mod = 1e9 + 7;

int pos[7001];
ll v[7001], b[7001];

bool check(int x,int y){
    return ((x|v[y]) == x);
}

void solve()
{
    int n;cin>>n;
    for(int i=0;i<n;i++) cin>>v[i];
    for(int i=0;i<n;i++) cin>>b[i];
    ll all = 0, pos = 0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(v[i]==v[j]){
                all |= v[i];
                pos = 1;
            }
        }
    }
    if(pos==0){
        cout<<0;
        return;
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        if(check(all,i)) ans+=b[i];
    }
    cout<<ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << fixed << setprecision(11);
    clock_t beg=clock();
    int t=1, n;
    // cin>>t;
    // n = t;
    while(t--){
        // cout << "Case #" << n-t << ": ";
        solve();
    }
    //
    clock_t end=clock();
    fprintf(stderr, "%lf sec\n", (double)(end-beg)/(CLOCKS_PER_SEC));
    return 0;
}


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

#define ll long long
#define sz(a) ((int)a.size())
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define dbg(a) cout<<#a<<" = "<<a<<endl
#define dbg2(a,b) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<endl
#define dbg3(a,b,c) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<endl
#define dbg4(a,b,c,d) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<", "<<#d<<" = "<<d<<endl
#define ld long double
#define pii pair<int,int>
#define vi vector<int>

const int mod = 1e9 + 7;

int a[7001][60];

bool check(int x,int y){
    for(int i=0;i<60;i++){
        if(a[y][i]==1 && a[x][i]==0) return 0;
    }
    return 1;
}

void solve()
{
    int n;cin>>n;
    ll v[n], b[n];
    int pos[n];
    for(int i=0;i<n;i++) cin>>v[i];
    for(int i=0;i<n;i++) cin>>b[i];
    for(int i=0;i<n;i++){
        ll x = v[i];
        pos[i] = 0;
        for(int j=0;j<60;j++){
            a[i][j] = x&1;
            x>>=1;
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(v[i]==v[j]) pos[i]=pos[j]=1;
        }
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        ll z=0;
        if(pos[i]==0) continue;
        for(int j=0;j<n;j++){
            if(check(i,j)) z+=b[i];
        }
        ans = max(ans,z);
    }
    cout<<ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << fixed << setprecision(11);
    clock_t beg=clock();
    int t=1, n;
    // cin>>t;
    // n = t;
    while(t--){
        // cout << "Case #" << n-t << ": ";
        solve();
    }
    //
    clock_t end=clock();
    fprintf(stderr, "%lf sec\n", (double)(end-beg)/(CLOCKS_PER_SEC));
    return 0;
}


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

#define ll long long
#define sz(a) ((int)a.size())
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(),v.end()
#define dbg(a) cout<<#a<<" = "<<a<<endl
#define dbg2(a,b) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<endl
#define dbg3(a,b,c) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<endl
#define dbg4(a,b,c,d) cout<<#a<<" = "<<a<<", "<<#b<<" = "<<b<<", "<<#c<<" = "<<c<<", "<<#d<<" = "<<d<<endl
#define ld long double
#define pii pair<int,int>
#define vi vector<int>

const int mod = 1e9 + 7;

int a[7001][60];

bool check(int x,int y){
    for(int i=0;i<60;i++){
        if(a[y][i]==1 && a[x][i]==0) return 0;
    }
    return 1;
}

void solve()
{
    int n;cin>>n;
    ll v[n], b[n];
    int pos[n];
    for(int i=0;i<n;i++) cin>>v[i];
    for(int i=0;i<n;i++) cin>>b[i];
    for(int i=0;i<n;i++){
        ll x = v[i];
        pos[i] = 0;
        for(int j=0;j<60;j++){
            a[i][j] = x&1;
            x>>=1;
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(v[i]==v[j]) pos[i]=pos[j]=1;
        }
    }
    ll ans=0;
    for(int i=0;i<n;i++){
        ll z=0;
        if(pos[i]==0) continue;
        for(int j=0;j<n;j++){
            if(check(i,j)) z+=b[j];
        }
        ans = max(ans,z);
    }
    cout<<ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cout << fixed << setprecision(11);
    clock_t beg=clock();
    int t=1, n;
    // cin>>t;
    // n = t;
    while(t--){
        // cout << "Case #" << n-t << ": ";
        solve();
    }
    //
    clock_t end=clock();
    fprintf(stderr, "%lf sec\n", (double)(end-beg)/(CLOCKS_PER_SEC));
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <iomanip>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

#define ull unsigned long long
#define ll long long
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define PI acos(-1.0)
const int N=1e5 + 5;
const int M=1e5 + 5;

int main()
{
    ios_base::sync_with_stdio(false);
    int n,i,j;
    ll a[N],b[N];
    cin>>n;
    map <ll,int> fre;
    for(i=0;i<n;i++){
        cin>>a[i];
        fre[a[i]]++;
    }
    bool tkn[N];
    vector < pair < ll , pair < ll , int > > > v;
    for(i=0;i<n;i++){
        tkn[i]=false;
    }
    for(i=0;i<n;i++){
        cin>>b[i];
        v.pb(mkp(a[i],mkp(b[i],i)));
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    vector <ll> f;
    ll ans = 0;
    for(i=0;i<n;i++){
        if(fre[a[i]]>1){
            tkn[i]=true;
            ans += b[i];
            f.pb(a[i]);
        }
    }
    //cout << ans << endl;
    for(i=0;i<n;i++){
        int id = v[i].se.se;
        if(tkn[id]) continue;
        //cout << id << endl;
//        if(id==1) cout
        //cout << id << endl;
        int lu=f.size();
        for(j=0;j<lu;j++){
            if((f[j]&a[id])==a[id]){
                ans+=b[id];
                tkn[id]=true;
                f.pb(a[id]);
                break;
            }
        }
    }
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <iomanip>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

#define ull unsigned long long
#define ll long long
#define mod 1000000007
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define PI acos(-1.0)
const int N=1e5 + 5;
const int M=1e5 + 5;

int main()
{
    ios_base::sync_with_stdio(false);
    int n,i,j;
    ll a[N],b[N];
    cin>>n;
    map <ll,int> fre;
    for(i=0;i<n;i++){
        cin>>a[i];
        fre[a[i]]++;
    }
    bool tkn[N];
    vector < pair < ll , pair < ll , int > > > v;
    for(i=0;i<n;i++){
        tkn[i]=false;
    }
    for(i=0;i<n;i++){
        cin>>b[i];
        v.pb(mkp(a[i],mkp(b[i],i)));
    }
    sort(v.begin(),v.end());
    reverse(v.begin(),v.end());
    vector <int> f;
    ll ans = 0;
    for(i=0;i<n;i++){
        if(fre[a[i]]>1){
            tkn[i]=true;
            ans += b[i];
            f.pb(a[i]);
        }
    }
    //cout << ans << endl;
    for(i=0;i<n;i++){
        int id = v[i].se.se;
        if(tkn[id]) continue;
        //cout << id << endl;
//        if(id==1) cout
        //cout << id << endl;
        int lu=f.size();
        for(j=0;j<lu;j++){
            if((f[j]&a[id])==a[id]){
                ans+=b[id];
                tkn[id]=true;
                f.pb(a[id]);
                break;
            }
        }
    }
    cout << ans << endl;

    return 0;
}


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

int main() {
	int n;
	cin >> n;
	long long a[n];
	long long b[n];
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	for(int i = 0; i < n; i++) {
		cin>>b[i];
	}
	// map<long long,pair<long long,long long>> m;
	// for(int i=0;i<n;i++){
	// 	if(m.find(a[i]) == m.end()){
	// 		m[a[i]] = make_pair(1,b[i]);
	// 	}
	// 	else{
	// 		m[a[i]].first++;
	// 		m[a[i]].second += b[i];
	// 	}
	// }
	// long long maximum = -1;
	// for(auto i : m){
	// 	cout<<i.first<<" "<<i.second.first<<" "<<i.second.second<<endl;
	// 	if(i.second.first>1 && i.first>maximum){
	// 		maximum = i.first;
	// 	}
	// }
	// cout<<maximum<<endl;
	// if(maximum == -1)
	// 	cout<<"0"<<endl;
	// else{
	// 	long long ans = m[maximum].second;
	// 	for(auto j:m){
	// 		if(j.first<maximum){
	// 			cout<<m[j.first].second<<endl;
	// 			ans += m[j.first].second;
	// 		}
	// 	}
	// 	cout<<ans<<endl;
	// }

	long long sum = 0;
	set<long long> s;
	long long maxIn = 0;
	for(int i =0;i<n;i++)
	{
		int tmp = a[i];
		for(int j = 0; j<n; j++)
		{
			if(a[i]==a[j] && i!=j)
			{
				sum += b[i];

				s.insert(a[i]);
			}
		}
	}
    if(s.size()<=1){
    	cout << 0;
    	return 0;
    }
//sum =0;
    for(int i = 0 ; i < n; i++)
    {   bool flag = false;

    	for(auto j : s) {
    	//	cout<<j<<endl;
     		if(a[i] == j)
     		break;
     		if( ((a[i] & j) == a[i])) {
				 flag = true;
				 break;
    		}
    	}

    	if(flag ==true)
    	{
    	//	cout<<i<<" "<<a[i]<<endl;
    		sum += b[i];
    	}
    }
    if( s.size() > 1) {
    	    //	cout << "size"<<s.size()<<" "<<0<<endl;

    	cout << sum;
    }
    else
    cout <<0;
	return 0;
}


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

int main() {
	int n;
	cin >> n;
	long long a[n];
	long long b[n];
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	for(int i = 0; i < n; i++) {
		cin>>b[i];
	}
	// map<long long,pair<long long,long long>> m;
	// for(int i=0;i<n;i++){
	// 	if(m.find(a[i]) == m.end()){
	// 		m[a[i]] = make_pair(1,b[i]);
	// 	}
	// 	else{
	// 		m[a[i]].first++;
	// 		m[a[i]].second += b[i];
	// 	}
	// }
	// long long maximum = -1;
	// for(auto i : m){
	// 	cout<<i.first<<" "<<i.second.first<<" "<<i.second.second<<endl;
	// 	if(i.second.first>1 && i.first>maximum){
	// 		maximum = i.first;
	// 	}
	// }
	// cout<<maximum<<endl;
	// if(maximum == -1)
	// 	cout<<"0"<<endl;
	// else{
	// 	long long ans = m[maximum].second;
	// 	for(auto j:m){
	// 		if(j.first<maximum){
	// 			cout<<m[j.first].second<<endl;
	// 			ans += m[j.first].second;
	// 		}
	// 	}
	// 	cout<<ans<<endl;
	// }

	long long sum = 0;
	set<long long> s;
	long long maxIn = 0;
	for(int i =0;i<n;i++)
	{
		int tmp = a[i];
		for(int j = 0; j<n; j++)
		{
			if(a[i]==a[j] && i!=j)
			{
				sum += b[i];

				s.insert(a[i]);
			}
		}
	}

    if(s.size()<1){
    	cout << 0;
    	return 0;
    }
//sum =0;
    for(int i = 0 ; i < n; i++)
    {   bool flag = false;

    	for(auto j : s) {
    	//	cout<<j<<endl;
     		if(a[i] == j)
     		break;
     		if( ((a[i] & j) == a[i])) {
				 flag = true;
				 break;
    		}
    	}

    	if(flag ==true)
    	{
    	//	cout<<i<<" "<<a[i]<<endl;
    		sum += b[i];
    	}
    }
    if( s.size() >= 1) {
    	    //	cout << "size"<<s.size()<<" "<<0<<endl;

    	cout << sum;
    }
    else
    cout <<0;
	return 0;
}


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

int main() {
	int n;
	cin >> n;
	long long a[n];
	long long b[n];
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	for(int i = 0; i < n; i++) {
		cin>>b[i];
	}
	long long sum = 0;
	map<long long,long long> m;
	vector<long long> v;
	for(int i = 0; i<n; i++)
	{
		++m[a[i]];
	}
	for(auto i : m )
	{
		if(i.second >1)
		  {
			v.push_back(i.first);
          }
	}

    for(int i = 0 ; i < n; i++)
    {
    	for(auto j : v) {
     		if((a[i]&j) == a[i]){
     			sum+=b[i];
     			break;
     		}

    	}

    }

   	cout << sum;

	return 0;
}


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

int main() {
	int n;
	cin >> n;
	long long a[n];
	long long b[n];
	vector<long long> group;
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	for(int i = 0; i < n; i++) {
		cin>>b[i];
	}
	long long sum = 0;
	set<long long> s;
	long long maxIn = 0;
	for(int i =0;i<n;i++)
	{
		int tmp = a[i];
		for(int j = 0; j<n; j++)
		{
			if(a[i]==a[j] && i!=j)
			{
				sum += b[i];
			    //cout<<a[i]<<" "<<"sum"<<"\n";
				group.push_back(a[i]);
			}
		}
	}
    if(group.size()<=1){
    	cout << 0;
    	return 0;
    }
	maxIn=group[0];
	for(int i = 1 ; i < group.size(); i++) {
    	maxIn = min( maxIn, group[i]);
    }
    // for(int i = 0 ; i < group.size(); i++) {
    // 	cout<<group[i]<<" "<<"sum"<<"\n";
    // }

    for(int i = 0 ; i < n; i++)
    {   bool flag = false;
    	int count  = 0;
    	for(int j = 0; j<group.size(); j++) {
    		if(a[i] != group[j] && ((a[i] & group[j]) == a[i])) {
				flag = true;
				break;
    		}
    	}

    	if(flag ==true)
    	{
    		//cout<<a[i]<<" "<<i<<"\n";
    		sum += b[i];
    	}
    }
    if( group.size() > 1) {
    	cout << sum;
    }
    else
    cout <<0;
	return 0;
}


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

int main() {
	// your code goes here
	int n;
	cin >> n;
	long long a[n];
	long long b[n];
	vector<long long> group;
	for(int i = 0; i < n; i++){
		cin>>a[i];
	}
	for(int i = 0; i < n; i++){
		cin>>b[i];
	}
	long long sum = 0;
	set<long long> s;
	for(int i =0;i<n;i++)
	{
		int tmp = a[i];
		for(int j =0; j<n; j++)
		{
			if(a[i]==a[j] && i!=j)
			{
			//	cout<<"same "<<a[i]<<" ";
				sum += b[i];
				group.push_back(a[i]);
			//	s.inset(a[i]);
			//	maxIn =  min(maxIn,a[i]);
			}
		}
	}
	//cout<<endl;
    for(int i = 0 ; i < n; i++)
    {
    	bool flag = false;
        for(int j = 0; j<group.size(); j++)
        {
        	if(a[i]< group[j]){
        		flag = true;
        	}
        }
        if(flag) {
        	sum += 	b[i];
        }
    }
    if(group.size()>1){
    	cout << sum;
    }
    else
    cout <<0;
	return 0;
}


#include <bits/stdc++.h>

#define int long long
typedef long long ll;
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N];
bool vis[N];
unordered_map<int, int> now;
unordered_map<int, int> tp;
unordered_map<int, int> now1;

bool test(int a, int b) {
    for (int i = 0; i <= 62; i++) {
        if ((a & (1LL << i))) if (!(b & (1LL << i)))return 1;
    }
    return 0;
}

bool test(int t) {
    bool f = 0;
    for (auto x:now1) {
        if (t == x.first)continue;
        f = 1;
        if (!test(t, x.first))return 0;
    }
    return f;
}

vector<int> v;

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> a[i];
    for (int i = 1; i <= n; i++)cin >> b[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)tp[a[i]]++;
    for (int i = 1; i <= n; i++)
        if (tp[a[i]] > 1)
            ans += b[i], v.push_back(a[i]);
        else now[a[i]] += b[i], now1[a[i]] += b[i];
    while (1) {
        bool upd = 0;
        for (auto p:now) {
            if (test(p.first)) {
                now.erase(p.first);
                now1.erase(p.first);
                upd = 1;
                break;
            }
        }
        if (!upd)break;
    }
    if (now.size() == 1) {
        bool f = 0;
        for (auto p:now) {
            for (auto x:v) {
                if (!test(p.first, x))f = 1;
            }
            if (f)ans += p.second;
        }
    } else {
        for (auto p:now)ans += p.second;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

#define int long long
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N];
bool vis[N];
unordered_map<int, int> now;
unordered_map<int, int> tp;

bool test(int a, int b) {
    return (a & (a ^ b));
}

bool test(int t) {
    bool f = 0;
    for (auto x:now) {
        if (t == x.first)continue;
        f = 1;
        if (!test(t, x.first))return 0;
    }
    return f;
}

vector<int> v;

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> a[i];
    for (int i = 1; i <= n; i++)cin >> b[i];
    int ans = 0;
    for (int i = 1; i <= n; i++)tp[a[i]]++;
    for (int i = 1; i <= n; i++)
        if (tp[a[i]] > 1)
            ans += b[i], v.push_back(a[i]);
        else now[a[i]] += b[i];
    for (int i = 1; i <= n; i++) {
        if (tp[a[i]] == 1) {
            bool f = 0;
            for (auto p:v) {
                if (!test(a[i], p))f = 1;
            }
            if (f)v.push_back(a[i]), ans += b[i];
        }
    }
    cout << ans << endl;
}

/*****
5
3 3 6 12 9 9
1 2 4 8 16 32
*****/


#include <stdio.h>
#include <vector>
#include <stack>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <unordered_map>

using namespace std;

int n;
long long int a[8000];
long long int b[8000];

unordered_map<int, int> m;

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
        if (m.find(a[i]) == m.end()) m[a[i]] = 1;
        else m[a[i]]++;
    }
    for (int i = 0; i < n; i++) scanf("%lld", &b[i]);

    long long int answer = 0;
    for (auto &x: m) {
        if (x.second < 2) continue;
        long long int sum = 0;
        for (int i = 0; i < n; i++) {
            if ((a[i] & x.first) == a[i]) {
                sum += b[i];
            }
        }
        answer = max(answer, sum);
    }
    printf("%lld\n", answer);
}


import java.util.*;

public class Solution {

	public static void main(String[] args) {

		Scanner in=new Scanner(System.in);
		StringBuffer out=new StringBuffer();

		int n=in.nextInt();

		long m[]=new long[n+2];
		for(int i=1; i<=n; i++)
			m[i]=in.nextLong();

		Stack<Integer> stack=new Stack<>();

		long dpLeft[]=new long[n+2];
		stack.push(0);
		for(int i=1; i<=n; i++) {
			while(!stack.isEmpty() && m[stack.peek()]>m[i])
				stack.pop();
			dpLeft[i]=(i-stack.peek())*m[i]+dpLeft[stack.peek()];
			stack.push(i);
		}

		stack.clear();

		long dpRight[]=new long[n+2];
		stack.push(n+1);
		for(int i=n; i>=1; i--) {
			while(!stack.isEmpty() && m[stack.peek()]>m[i])
				stack.pop();
			dpRight[i]=(stack.peek()-i)*m[i]+dpRight[stack.peek()];
			stack.push(i);
		}

		int index=-1;
		long max=0L;
		for(int i=1; i<=n; i++)
			if(dpLeft[i]+dpRight[i]-m[i]>=max) {
				max=dpLeft[i]+dpRight[i]-m[i];
				index=i;
			}

		long ans[]=new long[n+2];

		max=Long.MAX_VALUE;
		for(int i=index; i<=n; i++) {
			max=Math.min(max, m[i]);
			ans[i]=max;
		}

		max=Integer.MAX_VALUE;
		for(int i=index; i>=1; i--) {
			max=Math.min(max, m[i]);
			ans[i]=max;
		}

		for(int i=1; i<=n; i++)
			out.append(ans[i]+" ");
		System.out.println(out);
	}
}


import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args)throws IOException {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        LinkedList<Integer> stack = new LinkedList<Integer>();
		int n = in.nextInt();
		int ct,ix;
		int arr[] = new int[n];
		long inc[] = new long[n];
		long dec[] = new long[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		long sum=0,val=0;
		for(int i=0;i<n;i++)
		{
			while(stack.size()!=0 && arr[stack.peekLast()]>=arr[i])
			{
				stack.pollLast();
			}
			if(stack.size()==0)
				inc[i]=(i+1)*arr[i];
			else{
				ct=i-stack.peekLast();
				inc[i]=inc[stack.peekLast()]+(ct*arr[i]);
			}
			stack.addLast(i);
		}
		stack = new LinkedList<Integer>();
		for(int i=n-1;i>=0;i--)
		{
			while(stack.size()!=0 && arr[stack.peekLast()]>=arr[i])
			{
				stack.pollLast();
			}
			if(stack.size()==0)
				dec[i]=(n-i)*arr[i];
			else{
				ct=Math.abs(i-stack.peekLast());
				dec[i]=dec[stack.peekLast()]+(ct*arr[i]);
			}
			stack.addLast(i);
		}
		sum=0;
		ix=-1;
		for(int i=0;i<n;i++)
		{
			val=arr[i];
			// out.print("i="+i+" ");
			if((i-1)>=0){
				val+=(inc[i]-arr[i]);
				// out.print(val+" ");
			}
			if((i+1)<n){
				val+=(dec[i]-arr[i]);
				// out.print(val);
			}
			if(val>sum)
			{
				ix=i;
			}
			sum=Math.max(sum,val);
		}
		for(int i=ix-1;i>=0;i--)
		{
			arr[i]=Math.min(arr[i+1],arr[i]);
		}
		for(int i=ix+1;i<n;i++)
		{
			arr[i]=Math.min(arr[i-1],arr[i]);
		}
		for(int i=0;i<n;i++)
		{
			out.print(arr[i]+" ");
		}
		out.println();
		out.close();
	}
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

}


import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args)throws IOException {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        LinkedList<Integer> stack = new LinkedList<Integer>();
		int n = in.nextInt();
		// int a = 1000000000;
		// long b = (long)a*a;
		// out.println("b="+b);
		int ct,ix;
		int arr[] = new int[n];
		long inc[] = new long[n];
		long dec[] = new long[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		long sum=0,val=0;
		for(int i=0;i<n;i++)
		{
			while(stack.size()!=0 && arr[stack.peekLast()]>=arr[i])
			{
				stack.pollLast();
			}
			if(stack.size()==0)
				inc[i]=(long)(i+1)*arr[i];
			else{
				ct=i-stack.peekLast();
				inc[i]=inc[stack.peekLast()]+((long)ct*arr[i]);
			}
			stack.addLast(i);
		}
		stack = new LinkedList<Integer>();
		for(int i=n-1;i>=0;i--)
		{
			while(stack.size()!=0 && arr[stack.peekLast()]>=arr[i])
			{
				stack.pollLast();
			}
			if(stack.size()==0)
				dec[i]=(long)(n-i)*arr[i];
			else{
				ct=Math.abs(i-stack.peekLast());
				dec[i]=dec[stack.peekLast()]+((long)ct*arr[i]);
			}
			stack.addLast(i);
		}
		sum=0;
		ix=-1;
		for(int i=0;i<n;i++)
		{
			val=inc[i]+dec[i]-(long)arr[i];
			if(val>sum)
				ix=i;
			sum=Math.max(sum,val);
		}
		for(int i=ix-1;i>=0;i--)
		{
			arr[i]=Math.min(arr[i+1],arr[i]);
		}
		for(int i=ix+1;i<n;i++)
		{
			arr[i]=Math.min(arr[i-1],arr[i]);
		}
		for(int i=0;i<n;i++)
		{
			out.print(arr[i]+" ");
		}
		out.println();
		out.close();
	}
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

}


import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args)throws IOException {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        LinkedList<Integer> stack = new LinkedList<Integer>();
		int n = in.nextInt();
		int ct,ix;
		int arr[] = new int[n];
		int inc[] = new int[n];
		int dec[] = new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		long sum=0,val=0;
		for(int i=0;i<n;i++)
		{
			while(stack.size()!=0 && arr[stack.peekLast()]>=arr[i])
			{
				stack.pollLast();
			}
			if(stack.size()==0)
				inc[i]=(i+1)*arr[i];
			else{
				ct=i-stack.peekLast();
				inc[i]=inc[stack.peekLast()]+(ct*arr[i]);
			}
			stack.addLast(i);
		}
		stack = new LinkedList<Integer>();
		for(int i=n-1;i>=0;i--)
		{
			while(stack.size()!=0 && arr[stack.peekLast()]>=arr[i])
			{
				stack.pollLast();
			}
			if(stack.size()==0)
				dec[i]=(n-i)*arr[i];
			else{
				ct=Math.abs(i-stack.peekLast());
				dec[i]=dec[stack.peekLast()]+(ct*arr[i]);
			}
			stack.addLast(i);
		}
		sum=0;
		ix=-1;
		for(int i=0;i<n;i++)
		{
			val=arr[i];
			// out.print("i="+i+" ");
			if((i-1)>=0){
				val+=(inc[i]-arr[i]);
				// out.print(val+" ");
			}
			if((i+1)<n){
				val+=(dec[i]-arr[i]);
				// out.print(val);
			}
			if(val>sum)
			{
				ix=i;
			}
			sum=Math.max(sum,val);
		}
		for(int i=ix-1;i>=0;i--)
		{
			arr[i]=Math.min(arr[i+1],arr[i]);
		}
		for(int i=ix+1;i<n;i++)
		{
			arr[i]=Math.min(arr[i-1],arr[i]);
		}
		for(int i=0;i<n;i++)
		{
			out.print(arr[i]+" ");
		}
		out.println();
		out.close();
	}
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }

}


import java.io.*;
import java.util.*;

public class Main {
  private static void execute(ContestReader reader, PrintWriter out) {
    int n = reader.nextInt();
    int[] ms = reader.nextInt(n);
    int[] answerList = new Solver(n, ms).solve();
    out.print(answerList[0]);
    for (int i = 1; i < n; i++) {
      out.print(" ");
      out.print(answerList[i]);
    }
    out.println();
  }

  public static void main(String[] args) {
    ContestReader reader = new ContestReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    execute(reader, out);
    out.flush();
  }
}

class SegmentTree {
  int n;
  int[] values;

  SegmentTree(int[] originalValues) {
    n = 1;
    while (n < originalValues.length) {
      n *= 2;
    }
    values = new int[2 * n - 1];
//    Arrays.fill(values, Integer.MAX_VALUE);
    for (int i = 0; i < originalValues.length; i++) {
      values[i + n - 1] = originalValues[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      values[i] = Math.min(values[2 * i + 1], values[2 * i + 2]);
    }
  }

  private int getRangeMin(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) {
      return Integer.MAX_VALUE;
    }
    if (a <= l && r <= b) {
      return values[k];
    }
    int vl = getRangeMin(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getRangeMin(a, b, 2 * k + 2, (l + r) / 2, r);
    return Math.min(vl, vr);
  }

  public int getRangeMin(int a, int b) {
    return getRangeMin(a, b, 0, 0, n);
  }
}

class Result {
  final int topIndex;
  final long score;

  Result(int topIndex, long score) {
    this.topIndex = topIndex;
    this.score = score;
  }
}

class Solver {
  final int n;
  final int[] ms;

  SegmentTree segmentTree;
  int[] answers;

  Solver(int n, int[] ms) {
    this.n = n;
    this.ms = ms;
  }

  int[] solve(int index) {
    int[] answerList = new int[n];
    answerList[index] = ms[index];
    for (int i = index - 1; i >= 0; i--) {
      answerList[i] = Math.min(answerList[i + 1], ms[i]);
    }
    for (int i = index + 1; i < n; i++) {
      answerList[i] = Math.min(answerList[i - 1], ms[i]);
    }
    return answerList;
  }

  Result solve(int minIndex, int maxIndex, int height) {
//    System.err.printf("%d %d %d\n", minIndex, maxIndex, height);
    /*
    if (maxIndex - minIndex <= 1) {
      return new Result(minIndex, 0);
    }
    */

    int minHeight = segmentTree.getRangeMin(minIndex, maxIndex);

    int nextMinIndex = minIndex;
    Result result = null;
    while (nextMinIndex < maxIndex) {
      while (nextMinIndex < maxIndex && ms[nextMinIndex] == minHeight) {
        nextMinIndex++;
      }
      if (nextMinIndex == maxIndex) {
        break;
      }
      int nextMaxIndex;
      if (segmentTree.getRangeMin(nextMinIndex, maxIndex) > height) {
        nextMaxIndex = maxIndex;
      } else {
        int nextMaxIndexMin = nextMinIndex + 1;
        int nextMaxIndexMax = maxIndex;
        while (nextMaxIndexMax - nextMaxIndexMin > 1) {
          int nextMaxIndexMid = (nextMaxIndexMin + nextMaxIndexMax) / 2;
          if (segmentTree.getRangeMin(nextMinIndex, nextMaxIndexMid) > height) {
            nextMaxIndexMin = nextMaxIndexMid;
          } else {
            nextMaxIndexMax = nextMaxIndexMid;
          }
        }
        nextMaxIndex = nextMaxIndexMin;
      }
      int nextHeight = segmentTree.getRangeMin(nextMinIndex, nextMaxIndex);
      Result tempTempResult = solve(nextMinIndex, nextMaxIndex, nextHeight);
      long v = (long)(nextHeight - height) * (long)(nextMaxIndex - nextMinIndex);
      Result tempResult = new Result(tempTempResult.topIndex, tempTempResult.score + v);
      if (result == null || result.score < tempResult.score) {
        result = tempResult;
      }
      nextMinIndex = nextMaxIndex;
    }
    if (result == null) {
      return new Result(minIndex, 0);
    } else {
      return result;
    }
  }

  int[] solve() {
    segmentTree = new SegmentTree(ms);
    Result result = solve(0, n, 0);
    return solve(result.topIndex);
  }
}

class ContestReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;

  ContestReader(InputStream in) {
    reader = new BufferedReader(new InputStreamReader(in));
  }

  public String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  public char nextChar() {
    return next().charAt(0);
  }

  public int nextInt() {
    return Integer.parseInt(next());
  }

  public long nextLong() {
    return Long.parseLong(next());
  }

  public double nextDouble() {
    return Double.parseDouble(next());
  }

  public String[] next(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = next();
    }
    return array;
  }

  public char[] nextChar(int n) {
    char[] array = new char[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextChar();
    }
    return array;
  }

  public int[] nextInt(int n) {
    int[] array = new int[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextInt();
    }
    return array;
  }

  public long[] nextLong(int n) {
    long[] array = new long[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLong();
    }
    return array;
  }

  public double[] nextDouble(int n) {
    double[] array = new double[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextDouble();
    }
    return array;
  }

  public char[] nextCharArray() {
    return next().toCharArray();
  }

  public int[][] nextInt(int n, int m) {
    int[][] matrix = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextInt();
      }
    }
    return matrix;
  }

  public char[][] nextChar(int n, int m) {
    char[][] matrix = new char[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextChar();
      }
    }
    return matrix;
  }

  public long[][] nextLong(int n, int m) {
    long[][] matrix = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextLong();
      }
    }
    return matrix;
  }

  public double[][] nextDouble(int n, int m) {
    double[][] matrix = new double[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextDouble();
      }
    }
    return matrix;
  }

  public char[][] nextCharArray(int n) {
    char[][] matrix = new char[n][];
    for (int i = 0; i < n; i++) {
      matrix[i] = next().toCharArray();
    }
    return matrix;
  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;

public class SkyScraper {

    public static void main(String [] args) {
        final FastScanner scanner = new FastScanner();
        int n = scanner.nextInt();
        double [] nums = scanner.readAsDouble(n);
        int [] sol = solve(nums);
        System.out.println(printArr(sol));
    }

    public static String printArr(int [] arr) {
        StringBuilder s = new StringBuilder();
        for (int i : arr) {
            s.append(i);
            s.append(" ");
        }
        return s.toString();
    }

    static int [] solve(double [] nums) {
        double [] leftToRight = new double[nums.length];
        double [] rightToLeft = new double[nums.length];
        Stack<Integer> stack = new Stack<>();

        // init dp
        leftToRight[0] = nums[0];
        stack.push(0);

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] >= nums[stack.peek()]) {
                leftToRight[i] = (nums[i] * (i - stack.peek())) + leftToRight[stack.peek()];
            } else {
                int last = 0;
                while (stack.size() > 0 && nums[stack.peek()] > nums[i]) {
                    stack.pop();
                }
                if (stack.size() == 0) {
                    leftToRight[i] = nums[i] * (i + 1);
                } else {
//                    System.out.println("last is " + last + " and i is " + i);
                    last = stack.peek();
                    leftToRight[i] = leftToRight[last] + (nums[i] * (i - last));
                }
            }
            stack.push(i);
        }

        stack.clear();
        rightToLeft[nums.length - 1] = nums[nums.length - 1];
        stack.push(nums.length - 1);

        double max = 0;
        int biggestIndex = 0;

        for (int i = nums.length - 1; i >= 0; i --) {
            if (nums[i] >= nums[stack.peek()]) {
                rightToLeft[i] = (nums[i] * Math.abs(i - stack.peek())) + rightToLeft[stack.peek()];
            } else {
                int last = 0;
                while (stack.size() > 0 && nums[stack.peek()] > nums[i]) {
                    stack.pop();
                }
                if (stack.size() == 0) {
                    rightToLeft[i] = nums[i] * (nums.length - i);
                } else {
                    last = stack.peek();
                    rightToLeft[i] = rightToLeft[last] + (nums[i] * Math.abs(i - last));
                }
            }
            stack.push(i);

            double curr = rightToLeft[i] - nums[i] + leftToRight[i];
            if (curr > max) {
                max = curr;
                biggestIndex = i;
            }
        }

        int [] sol = new int[nums.length];
        double x = nums[biggestIndex];
        sol[biggestIndex] = (int) x;
        for (int i = biggestIndex - 1; i >= 0; i--) {
            x = Math.min(x, nums[i]);
            sol[i] = (int) x;
        }

        x = nums[biggestIndex];
        for (int i = biggestIndex + 1; i < nums.length; i++) {
            x = Math.min(x, nums[i]);
            sol[i] = (int) x;
        }

//        System.out.println(Arrays.toString(leftToRight));
//        System.out.println(Arrays.toString(rightToLeft));

        return sol;
    }

    public String doubleToInt(double b) {
        String s = String.format("%.0f", b);
        return s;
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }

        double [] readAsDouble(int n) {
            double [] a = new double[n];
            for (int i=0; i<n; i++) a[i]=(double) nextInt();
            return a;
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;

public class SkyScraper {

    public static void main(String [] args) {
        final FastScanner scanner = new FastScanner();
        int n = scanner.nextInt();
        int [] nums = scanner.readArray(n);
        int [] sol = solve(nums);
        System.out.println(printArr(sol));
    }

    public static String printArr(int [] arr) {
        StringBuilder s = new StringBuilder();
        for (int i : arr) {
            s.append(i);
            s.append(" ");
        }
        return s.toString();
    }

    static int [] solve(int [] nums) {
        double [] leftToRight = new double[nums.length];
        double [] rightToLeft = new double[nums.length];
        Stack<Integer> stack = new Stack<>();

        // init dp
        leftToRight[0] = nums[0];
        stack.push(0);

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] >= nums[stack.peek()]) {
                leftToRight[i] = (nums[i] * (i - stack.peek())) + leftToRight[stack.peek()];
            } else {
                int last = 0;
                while (stack.size() > 0 && nums[stack.peek()] > nums[i]) {
                    stack.pop();
                }
                if (stack.size() == 0) {
                    leftToRight[i] = nums[i] * (i + 1);
                } else {
//                    System.out.println("last is " + last + " and i is " + i);
                    last = stack.peek();
                    leftToRight[i] = leftToRight[last] + (nums[i] * (i - last));
                }
            }
            stack.push(i);
        }

        stack.clear();
        rightToLeft[nums.length - 1] = nums[nums.length - 1];
        stack.push(nums.length - 1);

        double max = 0;
        int biggestIndex = 0;

        for (int i = nums.length - 1; i >= 0; i --) {
            if (nums[i] >= nums[stack.peek()]) {
                rightToLeft[i] = (nums[i] * Math.abs(i - stack.peek())) + rightToLeft[stack.peek()];
            } else {
                int last = 0;
                while (stack.size() > 0 && nums[stack.peek()] > nums[i]) {
                    stack.pop();
                }
                if (stack.size() == 0) {
                    rightToLeft[i] = nums[i] * (nums.length - i);
                } else {
                    last = stack.peek();
                    rightToLeft[i] = rightToLeft[last] + (nums[i] * Math.abs(i - last));
                }
            }
            stack.push(i);

            double curr = rightToLeft[i] - nums[i] + leftToRight[i];
            if (curr > max) {
                max = curr;
                biggestIndex = i;
            }
        }

        int [] sol = new int[nums.length];
        int x = nums[biggestIndex];
        sol[biggestIndex] = x;
        for (int i = biggestIndex - 1; i >= 0; i--) {
            x = Math.min(x, nums[i]);
            sol[i] = x;
        }

        x = nums[biggestIndex];
        for (int i = biggestIndex + 1; i < nums.length; i++) {
            x = Math.min(x, nums[i]);
            sol[i] = x;
        }

//        System.out.println(Arrays.toString(leftToRight));
//        System.out.println(Arrays.toString(rightToLeft));

        return sol;
    }

    public String doubleToInt(double b) {
        String s = String.format("%.0f", b);
        return s;
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}