题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2460
解题心得:就考察了一个线性基的性质,就是线性基中任选元素异或和不为 0 0 0。就利用这个性质,按照 b b b降序排序,然后在插入过程中,只要能插入进去答案就可以直接加上 b b b。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
ll n, cxx[maxn];
struct Node {
ll a, b;
bool operator < (const Node &x) const {
return b > x.b;
}
}node[maxn];
bool insert(ll x) {
for(int i=62;i>=0;i--) {
if(x&(1ll<<i)) {
if(cxx[i] == 0) {
cxx[i] = x;
break;
} else
x ^= cxx[i];
}
}
return x > 0;
}
int main() {
// freopen("1.in.txt", "r", stdin);
scanf("%lld", &n);
for(int i=1;i<=n;i++) {
scanf("%lld%lld", &node[i].a, &node[i].b);
}
sort(node+1, node+1+n);
ll ans = 0;
for(int i=1;i<=n;i++) {
if(insert(node[i].a)) {
ans += node[i].b;
}
}
printf("%lld\n", ans);
return 0;
}