Marcin and Training Camp

Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.

Let’s focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers ai and bi; bi is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2j) is set in the binary representation of ai. Otherwise, this bit is not set.

Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn’t know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.

Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?

Input
The first line contains one integer n (1≤n≤7000) — the number of students interested in the camp.

The second line contains n integers. The i-th of them is ai (0≤ai<260).

The third line contains n integers. The i-th of them is bi (1≤bi≤109).

Output
Output one integer which denotes the maximum sum of bi over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0.

Examples
Input
4
3 2 3 6
2 8 5 10
Output
15
Input
3
1 2 3
1 2 3
Output
0
Input
1
0
1
Output
0
Note
In the first sample test, it’s optimal to send the first, the second and the third student to the camp. It’s also possible to send only the first and the third student, but they’d have a lower sum of bi.

In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
题意:给了很多人 每个人会不同算法 只要他会 就必须要求其他(至少一个)人会 不然不组 (60个算法 用二进制表达),第二行就是这个人的价值 求最大价值 在可以组的情况下
唯一可以组的情况就是 这个技能 可以和另外的人对应 而且其他认技能 (剩下除了技能和它一样的)就只能是他子集
这样我们暴力把所有可以的 同时标记了他们的子集就好了。
(a|b)==b说明a是b子集(注意这个写多重输入会报错)

#include"cstring"
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"map"
#include"cmath"
using namespace std;
#define maxn 8010
map<long long,int>mp;
long long a[maxn],b[maxn];
int vst[maxn];
int main()
{
	long long n;
	scanf("%lld",&n);
	memset(vst,0,sizeof(vst));
	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++)
	{
		if(mp[a[i]]>1)
		{
			for(int j=1;j<=n;j++)
			{
				if((a[i]|a[j])==a[i])
				{
					vst[j]=1;
				}
			}
		}
	}
	long long sum=0;
	for(int i=1;i<=n;i++)
	{
		if(vst[i])
		{
			sum+=b[i];
		}
	}
	cout<<sum<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值