Codeforces Round #588 (Div. 2) D. Marcin and Training Camp

题目链接

Marcin is a coach in his university. There are nn 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 11 to nn. Each of them can be described with two integers aiai and bibi; bibi is equal to the skill level of the ii-th student (the higher, the better). Also, there are 6060 known algorithms, which are numbered with integers from 00 to 5959. If the ii-th student knows the jj-th algorithm, then the jj-th bit (2j2j) is set in the binary representation of aiai. Otherwise, this bit is not set.

Student xx thinks that he is better than student yy if and only if xx knows some algorithm which yy 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 nn (1≤n≤70001≤n≤7000) — the number of students interested in the camp.

The second line contains nn integers. The ii-th of them is aiai (0≤ai<2^60).

The third line contains nn integers. The ii-th of them is bibi (1≤bi≤10^9).

Output

Output one integer which denotes the maximum sum of bibi 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

Copy

4
3 2 3 6
2 8 5 10

output

Copy

15

input

Copy

3
1 2 3
1 2 3

output

Copy

0

input

Copy

1
0
1

output

Copy

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 bibi.

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.

题意:

If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of aiai.这句话的意思是:用60位二进制中的1表示会的算法,0是不会,然后将二进制转化成十进制。(1010意思是会第4道和第2道,ai=10)

从n个人中选拔可以平静工作的队员,最少两人。只有一个题你会而他不会时,你会觉得你比他优秀。反之亦然,当每一个人不觉得自己比其它人都优秀时,他们可以平静的工作。(1100和111相互不服,1100和1100可平静工作并且1000和0100可加入他们。)求所有可以平静工作的人的b值之和。

思路:

没有最优秀的人,必然至少有两个人会的算法一样,凡是有人会的算法都在他们会的之内可加入。

这时我想到了或运算(以相等的数作为遍历条件,假设有两人会1010,则

1010|1000=1010

1010|0010=1010

1010|0001=1011

可知凡是会的题在这两人会的题之内,或运算的结果为这两人会的题,否则不是。

代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct A{
	ll a,b;
}x[7010];
bool cmp(A x,A y){
	return x.a>y.a;
}
int ins[7010];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%I64d",&x[i].a);
	}
	for(int i=0;i<n;i++){
		scanf("%I64d",&x[i].b);
	}
	sort(x,x+n,cmp);
	ll ans=0;
	for(int i=1;i<n;i++){
		if(ins[i]==0&&ins[i-1]==0&&x[i].a==x[i-1].a){
			ans+=x[i].b+x[i-1].b;
			ll t=x[i].a;
			ins[i]=ins[i-1]=1;
			for(int j=i+1;j<n;j++){
				if(ins[j]==0&&(x[j].a|t)==t){
					ins[j]=1;
					ans+=x[j].b;
				}
			}
		}
	}
	printf("%I64d\n",ans);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章