将所有元素按照魔法值从大到小排序,然后依次试着往线性基里插入就完事了.
#include <cstdio> #include <algorithm> #define N 2000 #define M 62 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n; ll d[N]; struct Node { ll val,magic; Node(ll val=0,ll magic=0):val(val),magic(magic){} }e[N]; bool cmp(Node a,Node b) { return a.magic>b.magic; } int main() { ll re=0; int i,j; // setIO("input"); scanf("%d",&n); for(i=1;i<=n;++i) scanf("%lld%lld",&e[i].val,&e[i].magic); sort(e+1,e+1+n,cmp); for(i=1;i<=n;++i) { ll x=e[i].val,y=e[i].magic; for(j=M;j>=0;--j) { if(x&(1ll<<j)) { if(d[j]) x^=d[j]; else { d[j]=x; re+=y; break; } } } } printf("%lld\n",re); return 0; }