思路:找出所有m的子集,加到价值中,找出最大价值即可。
代码:
void solve(){int n, m;cin >> n >> m;vector<pii>a(n + 1);for(int i = 1;i <= n;i ++)cin >> a[i].first >> a[i].second;int ans = 0;auto check = [&](int x){int res = 0;for(int i = 1;i <= n;i ++){if((x & a[i].second) == a[i].second)res += a[i].first;}ans = max(ans, res);};for(int i = 29;i >= 1;i --){if((m >> i) & 1){check((m ^ (1 << i)) | ((1 << i) - 1));}}check(m);cout << ans << endl;
}