参考:点击打开链接
[code]:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 2e5+5;
int n,m,a[105];
LL dp[maxn];
int mabs(int x){ return x<0?-x:x; }
int main(){
int i,j,cas;
scanf("%d",&cas);
for(int T=1;T<=cas;T++){
scanf("%d",&n);
m = 0;
memset(dp,0,sizeof(dp));
for(i = 1;i <= n;i++){
scanf("%d",&a[i]);
m += a[i];
}
LL mask = (1LL<<(n/2+2))-1;
dp[0] = 1;
for(i = 1;i <= n;i++){
for(j = m/2;j >= a[i];j--){
dp[j] |= (dp[j-a[i]]<<1)&mask;
}
}
int cnt;bool flag;
for(j = m/2;j>=0;j--){
cnt = 0;flag = false;
while(dp[j]){
if((dp[j]&1)&&(mabs(n-2*cnt)==1||mabs(n-2*cnt)==0)){
flag = true;
}
cnt++;
dp[j]>>=1;
}
if(flag) break;
}
if(j > m-j) j = m-j;
printf("Case %d: %d %d\n",T,j,m-j);
}
return 0;
}