版权声明:欢迎转载(标记出处),写得不好还请多指教 https://blog.csdn.net/quan_tum/article/details/82688319
按魔力值降序排列,然后把按序插入线性基,如果与之前的序号异或和为0则不插入。把魔力值累加起来输出就行了。
至于贪心的证明…不会!!!
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct A{ll d,w;}a[1005];
bool cmp(A a,A b){return a.w>b.w;}
int n;ll ans,p[65];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%lld%lld",&a[i].d,&a[i].w);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;++i){
ll x=a[i].d;
for(int j=61;j>=0;--j){
if(!(x&(1ll<<j))) continue;
if(!p[j]) {p[j]=x;break;}
x^=p[j];
}if(x) ans+=a[i].w;
}return !printf("%lld",ans);
}