xynuoj good bro

Brothers

Time Limit:  1 Sec   Memory Limit:  64 MB

Topic description

 After Xiao Ming and Xiao Mingming played the game, in order to take care of Xiao Mingming's game experience, Xiao Ming planned to distribute the won game coins to Xiao Mingming. These game coins have different denominations. He wants to divide these game coins into two parts and minimize the difference between the two game coins (that is, the difference between the sum of the game coins obtained by Xiao Ming and Xiao Mingming is the smallest), Now that the value of each game coin is known, they have been divided, do you know how much the difference between the game coin scores they get in the end?

enter

The first line has only one integer m (m<=1000), which represents the number of test data groups. Next, there is an integer n (n<=1000), which represents the number of game coins . Then there are n integers Vi (Vi<=100), which represent the score of the i-th game coin .

output

Output the difference, each group of outputs occupies one line.

sample input

2
5
2 6 5 8 9
3
2 1 5

Sample output

0
2

hint

This is a simple 01 knapsack problem, except that the knapsack capacity becomes half of the sum

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[100000];
int main(){
	int m,n;
	int a[1010];
	scanf("%d",&m);
	while(m--){
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		int sum=0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		for(int i=0;i<n;i++){
			for(int j=sum/2;j>=a[i];j--){
				dp[j]=max(dp[j],dp[j-a[i]]+a[i]);
			}
		}
		printf("%d\n",sum-2*dp[sum/2]);
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846945&siteId=291194637