Divide the coin into two heaps to minimize the difference between the two heaps of coins.
0-1 backpack, which can calculate all the sum of coins, and then start to explore 0 from Sum/2 (sum is the sum of all coins, until you find the biggest coin and I that can be made up by the given coin, and the minimum difference is sum-2 * I.
The Code is as follows:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;const int Maxn = 102;int coin[Maxn], dp[Maxn*500];int main(){#ifdef test freopen("input.txt", "r", stdin);#endif int m, n; scanf("%d", &n); while(n--) { int sum = 0; scanf("%d", &m); for(int i=0; i<m; ++i) { scanf("%d", &coin[i]); sum += coin[i]; } memset(dp, 0, sizeof(dp)); dp[0] = 1; for(int i=0; i<m; ++i) for(int j=sum; j>=coin[i]; --j) { if(dp[j-coin[i]]) dp[j] = 1; } int i; for(i=sum/2; i>=0; --i) if(dp[i]) break; printf("%d\n", sum - 2 * i); } return 0;}