Tenka1 Programmer Contest-D

来源:互联网 发布:单片机电子时钟设计 编辑:程序博客网 时间:2024/05/16 23:55

Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement
Seisu-ya, a store specializing in non-negative integers, sells N non-negative integers. The i-th integer is Ai and has a utility of Bi. There may be multiple equal integers with different utilities.

Takahashi will buy some integers in this store. He can buy a combination of integers whose bitwise OR is less than or equal to K. He wants the sum of utilities of purchased integers to be as large as possible.

Find the maximum possible sum of utilities of purchased integers.

Constraints
1≤N≤105
0≤K<230
0≤Ai<230(1≤i≤N)
1≤Bi≤109(1≤i≤N)
All input values are integers.
Inputs
Input is given from Standard Input in the following format:

N K
A1 B1
:
AN BN
Outputs
Print the maximum possible sum of utilities of purchased integers.

Sample Input 1
Copy
3 5
3 3
4 4
2 5
Sample Output 1
Copy
8
Buy 2 and 3 to achieve the maximum possible total utility, 8.

Sample Input 2
Copy
3 6
3 3
4 4
2 5
Sample Output 2
Copy
9
Buy 2 and 4 to achieve the maximum possible total utility, 9.

Sample Input 3
Copy
7 14
10 5
7 4
11 4
9 8
3 6
6 2
8 9
Sample Output 3
Copy
32

#include<bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN=1e5+5;int a[MAXN];LL b[MAXN];int main(){    int n,k;    while(scanf("%d%d",&n,&k)!=EOF)    {        for(int i=1;i<=n;i++)            scanf("%d%lld",&a[i],&b[i]);        LL ans=0,sum=0;        int tmp=k;        tmp^=((1<<30)-1);        for(int i=1;i<=n;i++)        {            if((tmp&a[i])==0) sum+=b[i];        }        ans=max(ans,sum);        for(int bit=0;bit<=30;bit++)        {            sum=0; tmp=k;            if(tmp&(1<<bit))            {                tmp^=(1<<bit);                tmp|=((1<<bit)-1);                tmp^=((1<<30)-1);                for(int i=1;i<=n;i++)                {                    if((tmp&a[i])==0) sum+=b[i];                }                ans=max(ans,sum);            }        }        printf("%lld\n",ans);    }    return 0;}
原创粉丝点击