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

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 (1n70001≤n≤7000) — the number of students interested in the camp.

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

The third line contains nn integers. The ii-th of them is bibi (1bi1091≤bi≤109).

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.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>

//线段树
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r


#define gcd __gcd
#define mem(s,t) memset(s,t,sizeof(s))
#define debug(a,n) for(int i=0;i<n;i++) cout<<" "<<a[i];  cout<<endl;
#define Debug(a,n,m) for(int i=0;i<n;i++) {for(int j=0;j<m;j++) cout<<a[i][j]<<" ";   cout<<endl; }
#define rep(j,k) for (int i = j;   i < k;  i++)
#define per(j,k) for (int i = j-1; i >= k; i--)
#define input(a,k) for (int i = 1; i <= (int)(k); i++)  {scanf("%d",&a[i]) ; }
#define INPUT(a,n,m) for(int i=0;i<n;i++) {for(int j=0;j<m;j++) cin>>a[i][j] ; }
#define TLE std::ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);   cout.precision(10);
    #define fi      first
    #define se      second
    #define pb      push_back 
    #define pql     priority_queue<lli>
    #define pq      priority_queue<int>
    #define ok      return 0;
    #define oi(x)   cout<<x<<endl;
    #define os(str) cout<<string(str)<<endl;
using namespace std;
inline void No()  {    printf("NO\n"); }
inline void Yes() {    printf("YES\n");}


typedef long long lli;
typedef long double ld;

  const int  N         = 300002;
  const int  logN      = (int)log2(N)+1;
  const int  MOD7      = 1000000007;
  const lli  MOD9      = 998244353;
  const lli  MODL      = 1000000007;
  const int  INF       = 1000000007;
  const lli  INFL      = 4ll*1000000007ll*1000000007ll;
  const ld   PI        = acos(-1);
  const int  HPRIME    = 31;
  const ld   EPS       = 1e-10;
  typedef pair < int, int > pii;
  typedef pair < lli, lli > pll;
  typedef vector < lli > vl;
  typedef vector < int > vi;
  

int n;
lli ans;
struct node 
{
    lli x,y;
    
}dp[7000+5],no;

int cmp(const node &a,const node &b)
{
    if(a.x!=b.x)   return a.x<b.x;
    return a.y<b.y;
}
bool vis[7000+5];
//#define LOCAL
lli AC(lli k)
{
    lli cnt = 0;
    for(int i=0;i<n;i++)
    {
        no = dp[i];
        if( (no.x|k) == k && !vis[i] )
        {
            cnt += no.y;
            vis[i] = true;
        }
    }
    return cnt;
}

int main()
{
    TLE;
#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif
    while(cin>>n)
    {
        mem(vis,0);   ans = 0;
        rep(0,n) cin>>dp[i].x;
        rep(0,n) cin>>dp[i].y;
        sort(dp,dp+n,cmp);
        for(int i=0;i<n-1;i++)
        {
            if(dp[i].x==dp[i+1].x)
            {
                if(!vis[i])
                {
                    ans += AC(dp[i].x);
                }
            }
        }
        cout<<ans<<endl;
    }
    ok;
}

 或者

struct node 
{
    lli x,y;
    bool operator < (const node &now)const
    {
        if(x!=now.x)
            return x < now.x;
        return y<now.y;
    }
}dp[7000+5],no;

sort(dp,dp+n);

猜你喜欢

转载自www.cnblogs.com/Shallow-dream/p/11600188.html