题解:P16964 [SCCPC 2026] 最大权独立集问题
CJY_Holmes · · 题解
思路
定义函数
有定理:
所以我们只需要把每个
对两类分别求和取最大值即可。
Code
#include<bits/stdc++.h>
using namespace std;
int T,n;
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>T;
while(T--){
cin>>n;
long long s1=0,s2=0;
while(n--){
int w;cin>>w;
if(__builtin_parity(w)) s1+=w;
else s2+=w;
}
cout<<max(s1,s2)<<'\n';
}
}