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 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<2^60).
The third line contains n integers. The i-th of them is bi (1≤bi≤10^9).
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.
思路:
首先理解一下题意,不能有别人不会的算法的同学在一起,所能够求的的最大的bi之和(这里要注意最大值至少要有两个,哪怕别人一人会一个算法,而一个人都会他们的,也不能的,必须要有一个一样的才行).
所以我们可以分析:对于至少有两个一模一样的,我们可以把它以及它的子集可以加进去(因为子集在里面嘛),所以我们就先找到至少两个的,然后用 | 运算符进行判断啦~
代码如下:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=1e4+10;
ll a[mx],b[mx];
map<ll,ll>p;//用map来统计此数
set<ll>q; //用set来统计加入的点
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
p[a[i]]++;//统计此数
}
for(int i=1;i<=n;i++)
scanf("%lld",&b[i]);
ll ans=0;
for(auto &it:p){
if(it.second>1){//如果此数大于1
for(int i=1;i<=n;i++){
if((it.first|a[i])==it.first)//这里直接用 | 运算符
q.insert(i);//set集合插入
}
}
}
for(auto &it:q)//直接加
ans+=b[it];
printf("%lld\n",ans);
return 0;
}