贪心一下,从大到小加,如何避免“魔法抵消”呢?
动态维护一个线性基就好了。
#include<bits/stdc++.h>
#define N 1000
#define LL long long
using namespace std;
struct stone{
LL a;
LL b;
void read()
{
scanf("%lld%lld",&a,&b);
}
};stone A[N+1];
LL n;
LL linear[65];
LL Ans;
bool cmp(const stone &A,const stone &B)
{
return A.b>B.b;
}
int main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)A[i].read();
sort(A+1,A+n+1,cmp);
for(int i=1;i<=n;i++)
{
for(int j=62;j>=0;j--)
{
if((A[i].a>>j)&1)
{
if(!linear[j]){linear[j]=A[i].a;break;}
else A[i].a^=linear[j];
}
}
if(A[i].a)Ans+=A[i].b;
}
cout<<Ans;
return 0;
}