2 5 2 6 5 8 9 3 2 1 5
0 2
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<string.h> 5 using namespace std; 6 const int MAX = 50005; 7 int dp[MAX],a[MAX]; 8 int main() 9 { 10 int T,n,i,j,sum; 11 scanf("%d",&T); 12 while(T--) 13 { 14 sum=0; 15 memset(dp,0,sizeof(dp)); 16 scanf("%d",&n); 17 for(i=0;i<n;i++) 18 { 19 scanf("%d",&a[i]); 20 sum+=a[i]; 21 } 22 for(i=0;i<n;i++) 23 { 24 for(j=sum/2;j>=a[i];j--) 25 { 26 if(dp[j]>dp[j-a[i]]+a[i]) 27 dp[j]=dp[j]; 28 else 29 dp[j]=dp[j-a[i]]+a[i]; 30 } 31 } 32 printf("%d\n",sum-2*dp[sum/2]); 33 } 34 return 0; 35 }
原文:http://www.cnblogs.com/caterpillarofharvard/p/4230227.html