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;
}

 

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